2010/05/14

pluggable Y Combinator 2

さっきの続き。
コードは以下のとおり。
(define (make-trace)
(lambda (f)
(lambda (n)
(format #t "\; call with : ~a\n" n)
(f n))))
(time ((Y (compose (make-trace) fib)) 5))
;(time ((Y (compose (make-trace) fib)) 5))
; real 0.000
; user 0.000
; sys 0.000
; call with : 5
; call with : 4
; call with : 3
; call with : 2
; call with : 1
; call with : 0
; call with : 1
; call with : 2
; call with : 1
; call with : 0
; call with : 3
; call with : 2
; call with : 1
; call with : 0
; call with : 1
8
(time ((Y (compose (make-memoize)(compose (make-trace) fib))) 5))
;(time ((Y (compose (make-memoize) (compose (make-trace) fib))) 5))
; real 0.000
; user 0.000
; sys 0.000
; call with : 5
; call with : 4
; call with : 3
; call with : 2
; call with : 1
; call with : 0
8
view raw trace.scm hosted with ❤ by GitHub

おもしろいな~。

追記

;(compose hoge (compose fuga ... は (compose hoge fuga ... で良かった。

(time ((Y (compose (make-memoize)(make-trace) fib)) 10))
;(time ((Y (compose (make-memoize) (make-trace) fib)) 10))
; real 0.000
; user 0.000
; sys 0.000
; call with : 10
; call with : 9
; call with : 8
; call with : 7
; call with : 6
; call with : 5
; call with : 4
; call with : 3
; call with : 2
; call with : 1
; call with : 0
89

プログラミングGauche

0 件のコメント:

コメントを投稿