next up previous
Next: 9.2 遅延評価の一般化 Up: 9 遅延評価(lazy evaluation) Previous: 9 遅延評価(lazy evaluation)

9.1 遅延評価の実現

ストリームを作る関数を以下のように定義する.

(defmacro stream-cons (object stream)
  `(list ,object #'(lambda () ,stream)))

(defun stream-first (stream)
  (first stream))

(defun stream-rest (strm)
  (funcall (second strm)))
実行例は以下のようにストリームにstream-restでアクセスにいった時に計算 がなされる.

> (setq a (stream-cons (expt 2 2)
                          (stream-cons (expt 2 3)
                                       'empty-stream)))

(4 (LAMBDA NIL (STREAM-CONS (EXPT 2 3) 'EMPTY-STREAM))) 
> (stream-rest a)

(8 (LAMBDA NIL 'EMPTY-STREAM)) 
> a

(4 (LAMBDA NIL (STREAM-CONS (EXPT 2 3) 'EMPTY-STREAM))) 
> (stream-rest (stream-rest a))

EMPTY-STREAM 
> a

(4 (LAMBDA NIL (STREAM-CONS (EXPT 2 3) 'EMPTY-STREAM)))


generated through LaTeX2HTML. M.Inaba 平成18年5月7日