Deprecated: Creation of dynamic property wpdb::$categories is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/wp-db.php on line 668

Deprecated: Creation of dynamic property wpdb::$post2cat is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/wp-db.php on line 668

Deprecated: Creation of dynamic property wpdb::$link2cat is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/wp-db.php on line 668

Deprecated: Return type of WP_Theme::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-theme.php on line 554

Deprecated: Return type of WP_Theme::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-theme.php on line 595

Deprecated: Return type of WP_Theme::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-theme.php on line 535

Deprecated: Return type of WP_Theme::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-theme.php on line 544

Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /usr/home/bugyo/public_html/b-log/wp-includes/comment-template.php on line 1739

Deprecated: Return type of WP_REST_Request::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/rest-api/class-wp-rest-request.php on line 960

Deprecated: Return type of WP_REST_Request::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/rest-api/class-wp-rest-request.php on line 980

Deprecated: Return type of WP_REST_Request::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/rest-api/class-wp-rest-request.php on line 992

Deprecated: Return type of WP_REST_Request::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/rest-api/class-wp-rest-request.php on line 1003

Deprecated: Return type of WP_Block_List::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 151

Deprecated: Return type of WP_Block_List::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 175

Deprecated: Return type of WP_Block_List::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 164

Deprecated: Return type of WP_Block_List::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 186

Deprecated: Return type of WP_Block_List::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 138

Deprecated: Return type of WP_Block_List::offsetExists($index) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 75

Deprecated: Return type of WP_Block_List::offsetGet($index) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 89

Deprecated: Return type of WP_Block_List::offsetSet($index, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 110

Deprecated: Return type of WP_Block_List::offsetUnset($index) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 127

Deprecated: Return type of WP_Block_List::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 199

Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/wp-db.php on line 3030

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Warning: Cannot modify header information - headers already sent by (output started at /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php:14) in /usr/home/bugyo/public_html/b-log/wp-includes/feed-rss2.php on line 8
Scheme – B-LOG https://blog.bugyo.tk/b-log bugyoのlog。決してblogではない。 Sun, 26 Aug 2012 12:35:38 +0000 en-US hourly 1 https://wordpress.org/?v=5.7.2 [Lisp] Common LispとSchemeの関数対応表 https://blog.bugyo.tk/b-log/archives/822 https://blog.bugyo.tk/b-log/archives/822#respond Tue, 22 Nov 2011 06:32:54 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 383
http://blog.bugyo.tk/b-log/?p=822 Continue reading [Lisp] Common LispとSchemeの関数対応表]]> Common Lispの関数やマクロと対応する機能を持つSchemeの手続きや構文を表にまとめる.

随時更新.

Common Lisp Scheme 備考
atom なし (not (pair? obj))を使う
consp pair?
dolist for-each 記法は異なる
mapcan append-map!  SRFI 1
mapcar map
multiple-value-bind receive SRFI 8, library syntax
null null?
nth list-ref
nthcdr list-tail SRFI 1ではdrop
progn begin
rplaca set-car!
rplacd set-cdr!
setq set!
]]>
https://blog.bugyo.tk/b-log/archives/822/feed 0
[並行計算][Scheme][Racket] SRFI 18のmutexとcondition variable https://blog.bugyo.tk/b-log/archives/799 https://blog.bugyo.tk/b-log/archives/799#respond Wed, 09 Nov 2011 12:15:18 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 383
http://blog.bugyo.tk/b-log/?p=799 Continue reading [並行計算][Scheme][Racket] SRFI 18のmutexとcondition variable]]> RacketはSRFI 18に対応していることになっているが、mutex-lock!やcondition-variable-signal!などどの手続きはrequireしても定義されない。

mutexやcondition variableのような同期機構ではなく、チャネル通信などの機構を使えということなのだと思うが、ロックを使ったほうが素直に実装できる場合もある。

幸いセマフォはあるようなので、セマフォでmutexやcondition variableを次のように書いた。

;;; mutex
(define (make-mutex)
  (make-semaphore 1))

(define (mutex-lock! m)
  (semaphore-wait m))

(define (mutex-unlock-primitive! m)
  (semaphore-post m))

(define (mutex-unlock! m . rest)
  (let ((condvar (if (null? rest) #f (car rest))))
    (mutex-unlock-primitive! m)
    (when condvar
      (condition-variable-wait! condvar))))

;;; gate
(define (make-gate)
  (make-semaphore 0))

(define (gate-wait! g)
  (semaphore-wait g))

(define (gate-signal! g)
  (semaphore-post g))

;;; condition variable
(define (make-condition-variable)
  (list 'condition-variable '() (make-mutex)))

(define (condition-variable-gates cv)
  (list-ref cv 1))

(define (condition-variable-mutex cv)
  (list-ref cv 2))

(define (condition-variable-clear-gates! cv)
  (set-car! (cdr cv) '() ))

(define (condition-variable-put-gates! cv val)
  (set-car! (cdr cv)
	    (cons val (condition-variable-gates cv))))

(define (condition-variable-wait! cv)
  (let ((mutex (condition-variable-mutex cv))
	(gates (condition-variable-gates cv))
	(new-gate (make-gate)))
    (mutex-lock! mutex)
      (condition-variable-put-gates! cv new-gate)
    (mutex-unlock-primitive! mutex)
    (gate-wait! new-gate)))

(define (condition-variable-signal! cv)
  (let ((mutex (condition-variable-mutex cv))
	(gates (condition-variable-gates cv)))
    (mutex-lock! mutex)
      (for-each (lambda (g) (gate-signal! g))
		gates)
      (condition-variable-clear-gates! cv)
    (mutex-unlock-primitive! mutex)))

condition variableは使い捨てのmutexを用いて実装している。
これで、同期に関してSRFI 18で定められた手続きが使える。

]]>
https://blog.bugyo.tk/b-log/archives/799/feed 0
[Scheme][Racket] set-car! set-cdr! が使えない https://blog.bugyo.tk/b-log/archives/794 https://blog.bugyo.tk/b-log/archives/794#respond Tue, 08 Nov 2011 10:45:23 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 383
http://blog.bugyo.tk/b-log/?p=794 (require r5rs) とすると、set-car! や set-cdr! が定義され、cons で作ったペアに再代入できる。 参考: Getting rid of set-car! and set-cdr!]]> racket コマンドで起動した Racket では set-car! や set-cdr! が定義されていなかった。

調べてみると Racket ではペアへの破壊的な代入を許さないらしい。

代わりに mcons というデータ構造が用意されていてこちらは set-mcar! や set-mcdr! といった手続きで代入ができる。

R5RS に基づいて書かれた Scheme のプログラムを動かすにはペアを扱う手続きを mcons を使うものに定義し直す必要があるが、すでにそれをするパッケージが用意されている。

> (require r5rs)

とすると、set-car! や set-cdr! が定義され、cons で作ったペアに再代入できる。

参考: Getting rid of set-car! and set-cdr!

]]>
https://blog.bugyo.tk/b-log/archives/794/feed 0
[数式処理][Scheme] 算術式を簡単にする https://blog.bugyo.tk/b-log/archives/775 https://blog.bugyo.tk/b-log/archives/775#respond Sun, 06 Nov 2011 04:13:56 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 383
http://blog.bugyo.tk/b-log/?p=775 Continue reading [数式処理][Scheme] 算術式を簡単にする]]> 微分などで複雑になった算術式を簡素化するには次のようにする。

(define (simple exp)
  (define (flat lst op)
    (append-map! (lambda (x)
                   (if (and (pair? x) (eq? (car x) op))
                       (flat (cdr x) op)
                       (list x)))
                 lst))
  
  (define (constant-fold args init op)
    (define (cf args num acc)
      (cond ((null? args)
             (values acc num))
            ((number? (car args))
             (cf (cdr args) (op num (car args)) acc))
            (else
             (cf (cdr args) num (append! acc (list (car args)))))))
    (cf args init '() ))
  
  (define (simple-+ args)
    (set! args (map simple args))
    (set! args (flat args '+))
    (receive (exps num) (constant-fold args 0 +)
     (cond ((null? exps) num)
           (else `(+ ,@args num)))))
  
  (define (simple-* args)
    (call/cc
     (lambda (c)
       (set! args (map simple args))
       (set! args (flat args '*))
       (map (lambda (x)
              (when (and (number? x) (= x 0))
                    (c 0)))
            args)
       (set! args (constant-fold args 1 *))
       (set! args (remove (lambda (x)
                            (and (number? x)(= x 1)))
                          args))
       (if (= 1 (length args))
           (car args)
           `(* ,@args)))))

  (define (simple-expt base ex)
    (set! base (simple base))
    (set! ex (simple ex))
    (cond ((or (and (number? base) (= base 1))
               (and (number? ex) (= ex 0)))
           1)
          ((or (and (number? base) (= base 0))
               (and (number? ex) (= ex 1)))
           base)
          (else
           `(expt ,base ,ex))))

  (define (simple-- arg)
    (cond ((number? arg) (- arg))
          ((and (pair? arg)
                (eq? (car arg) '+))
           (simple-+ (map (lambda (x) (list '- x))
                          (cdr arg))))
          ((and (pair? arg)
                (eq? (car arg) '*))
           (simple-* (map (lambda (x) (list '- x))
                          (cdr arg))))
          (else
           `(- ,arg))))
  
  (cond ((not (pair? exp)) exp)
        ((eq? (car exp) '-)
         (simple-- (cadr exp)))
        ((eq? (car exp) '+)
         (simple-+ (cdr exp)))
        ((eq? (car exp) '*)
         (simple-* (cdr exp)))
        ((eq? (car exp) 'expt)
         (simple-expt (cadr exp) (caddr exp)))
        (else
         `(,(car exp) ,@(map simple (cdr exp))))))
> (simple (deriv '(expt (* (sin x) (cos x)) 1/2) 'x))
=> (* (expt (* (sin x) (cos x)) -1/2) (+ (* (cos x) (cos x)) (* (sin x) (- (sin x)))) 1/2)

`-‘ 演算子は単項演算子としてのみ使える。

]]>
https://blog.bugyo.tk/b-log/archives/775/feed 0
[数式処理][Scheme] 微分する https://blog.bugyo.tk/b-log/archives/772 https://blog.bugyo.tk/b-log/archives/772#respond Sun, 06 Nov 2011 04:09:32 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 383
http://blog.bugyo.tk/b-log/?p=772 Continue reading [数式処理][Scheme] 微分する]]> 複雑な微分をするときは計算機に任せた方が良いように思う。

Scheme で微分するには次のようにする。

(define (deriv exp var)
  (cond ((eq? exp var) 1)
	((not (pair? exp)) 0)
	((eq? (car exp) '-)
	 `(- ,(deriv (cadr exp) var)))
	((eq? (car exp) '+)
	 `(+ ,(deriv (cadr exp) var) ,(deriv (caddr exp) var)))
	((eq? (car exp) '*)
	 `(+ (* ,(deriv (cadr exp) var)
		,(caddr exp))
	     (* ,(cadr exp)
		,(deriv (caddr exp) var))))
	((eq? (car exp) 'expt)
	 (let ((g (cadr exp))
	       (n (caddr exp)))
	   `(* (* ,n (expt ,g ,(- n 1))) ,(deriv g var))))
	((eq? (car exp) 'sin)
	 `(* (cos ,(cadr exp)) ,(deriv (cadr exp) var)))
	((eq? (car exp) 'cos)
	 `(* (- (sin ,(cadr exp))) ,(deriv (cadr exp) var)))
        ((eq? (car exp) 'tan)
         `(* (+ 1 (expt ,exp 2)) ,(deriv (cadr exp) var)))
	(else (error "not yet."))))
> (deriv '(expt (* (sin x) (cos x)) 1/2)) 'x)
=> (* (* 1/2
         (expt (* (sin x) (cos x)) -1/2))
      (+ (* (* (cos x) 1)
	    (cos x))
         (* (sin x)
	    (* (- (sin x)) 1))))

`-‘ 演算子は単項演算子としてのみ使える。

]]>
https://blog.bugyo.tk/b-log/archives/772/feed 0
[Scheme][Racket] 相対パスでロード https://blog.bugyo.tk/b-log/archives/766 https://blog.bugyo.tk/b-log/archives/766#respond Tue, 01 Nov 2011 14:36:01 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 383
http://blog.bugyo.tk/b-log/?p=766 Racketで相対パスを用いてロードする際の注意。

load で相対パスを使った場合は current-directory (デフォルトでは実行パス)を基点にロードパスが決まる。

ただ、”./filename” や “../filename” といった形式は使えなかった。
current-directory と結合してOSに丸投げするだけでいいのに、信じられない。

ロードされているファイルからそのファイルのあるディレクトリを基点に相対パスでロードする場合は load-relative を用いる。

 (load-relative "relative-path")

その際、基点となるディレクトリは current-load-relative-directory 関数で取得できる。

]]>
https://blog.bugyo.tk/b-log/archives/766/feed 0
[Scheme] 束縛する値を省略できるように let を書き換える https://blog.bugyo.tk/b-log/archives/733 https://blog.bugyo.tk/b-log/archives/733#respond Fri, 03 Jun 2011 07:58:49 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 383
http://blog.bugyo.tk/b-log/?p=733 (let (a b c) c) NIL のように let で束縛する値を省略できる. 省略された変数は nil に束縛される. しかし,Scheme にはそのような省略記法はない. そこで,マクロを使い Common Lisp のような略記法を導入する. 次のように書く. (define-syntax old-let let) (define-syntax %let (syntax-rules () ((_ (te ...) () body ...) (old-let (te ...) body Continue reading [Scheme] 束縛する値を省略できるように let を書き換える]]> Common Lisp では

CL-USER> (let (a b c) c)
NIL

のように let で束縛する値を省略できる.
省略された変数は nil に束縛される.

しかし,Scheme にはそのような省略記法はない.
そこで,マクロを使い Common Lisp のような略記法を導入する.
次のように書く.

(define-syntax old-let let)

(define-syntax %let
  (syntax-rules ()
    ((_ (te ...) () body ...)
     (old-let (te ...)
       body ...))
    ((_ (te ...) ((id e) be ...) body ...)
     (%let (te ... (id e)) (be ...) body ...))
    ((_ (te ...) (id be ...) body ...)
     (%let (te ... (id '())) (be ...) body ...))))

(define-syntax let
  (syntax-rules ()
    ((_ e1 e2 ...)
     (%let () e1 e2 ...))))

Common Lisp と同様に束縛する値を省略した変数は空リストに束縛されるようにしている.

]]>
https://blog.bugyo.tk/b-log/archives/733/feed 0
[Scheme] 循環のあるグラフをコピーする https://blog.bugyo.tk/b-log/archives/699 https://blog.bugyo.tk/b-log/archives/699#respond Tue, 01 Mar 2011 07:07:43 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 383
http://blog.bugyo.tk/b-log/?p=699 Continue reading [Scheme] 循環のあるグラフをコピーする]]> コンスセルを使った循環のあるグラフをコピーする関数は次のように書ける。

(define (copy-graph-sub ht graph)
  (cond ((not (pair? graph)) graph)
	((hash-table-exists? ht graph)
	 (hash-table-ref ht graph))
	(else
	 (let ((p (cons #f #f)))
	   (hash-table-set! ht graph p)
	   (set-car! p (copy-graph-sub ht (car graph)))
	   (set-cdr! p (copy-graph-sub ht (cdr graph)))
	   p))))

(define (copy-graph graph)
  (copy-graph-sub (make-hash-table) graph))

ハッシュテーブルの操作はSRFI-69に沿っている。
Gauche では hash-table-ref は hash-table-get, hash-table-set! は hash-table-put! と書くみたい。

]]>
https://blog.bugyo.tk/b-log/archives/699/feed 0
[Scheme][Gauche][SRFI-18] スレッドライブラリを使う https://blog.bugyo.tk/b-log/archives/678 https://blog.bugyo.tk/b-log/archives/678#respond Tue, 07 Dec 2010 09:35:59 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 383
http://blog.bugyo.tk/b-log/?p=678 Gauche で SRFI-18 に準拠したスレッドライブラリを使うには、

 (use gauche.threads)

として、 gauche.threads モジュールをロードする必要がある。

]]>
https://blog.bugyo.tk/b-log/archives/678/feed 0