CLAZY であるべきか
元は SICP です。
お試しのソースコード:
(defmacro delay (expr) `(lambda () ,expr)) (defun force (thunk) (funcall thunk)) (defmacro cons-stream (head tail) `(cons ,head (delay ,tail))) (defun head (lazy-stream) (car lazy-stream)) (defun tail (lazy-stream) (force (cdr lazy-stream))) (defun make-plus-stream (x y) (cons-stream x (make-plus-stream (+ x y) y))) (defun take-and-prn (stm b) (defun iter (s i) (if (>= i b) 'done (progn (format t "~A~%" (head s)) (iter (tail s) (+ i 1))))) (iter stm 0))
こんな風に無限で遊べます:
(setq stm (make-plus-stream 1 1)) =>(1 . #<lexical-closure: (anonymous)>) (take-and-prn stm 5) 1 2 3 4 5 =>done (setq stm (make-plus-stream 1 2)) =>(1 . #<lexical-closure: (anonymous)>) (take-and-prn stm 5) 1 3 5 7 9 =>done
| 固定リンク

コメント