Is there a reason why the following code runs out of memory when I call the function test? Works find on clisp and similar code in scheme.
I tried implementing delay/force two different ways that does not seem to matter. ;;try #1 ;; ;;Code from Peter Norvig ;; ;;see http://www.norvig.com/paip/auxfns.lisp ;; (defstruct delay value (computed? nil)) ;; (defmacro delay (&rest body) ;; "A computation that can be executed later by FORCE." ;; `(make-delay :value #'(lambda () . ,body))) ;; (defun force (delay) ;; "Do a delayed computation, or fetch its previously-computed value." ;; (if (delay-computed? delay) ;; (delay-value delay) ;; (prog1 (setf (delay-value delay) (funcall (delay-value delay))) ;; (setf (delay-computed? delay) t)))) ;;try #2 (defun make-promise (thunk) (let ((computed? nil) (value thunk)) (lambda () (if computed? value (setf computed? t value (funcall value)))))) (defmacro delay (&body body) `(make-promise (lambda () . ,body))) (defun force (delay) (funcall delay)) (defmacro lazy-cons (expr1 expr2) `(cons ,expr1 (delay ,expr2))) (defun lazy-car (lst) (car lst)) (defun lazy-cdr (lst) (force (cdr lst))) (defun integer-list (start) (lazy-cons start (integer-list (1+ start)))) (defun test () (do ((lst (integer-list 0) (lazy-cdr lst))) ((< 10000000 (lazy-car lst)) (lazy-car lst)))) ____________________________________________________ Start your day with Yahoo! - make it your home page http://www.yahoo.com/r/hs
