I want to implement the lazy stream in SICP section 3.5.1
Firstly, I defined this two functions
(defmacro delay (form)
`(lambda () ,form))
(defun force (form)
(when form
(funcall form)))
When we called:
(force (delay '(+ 1 2)))
;;=> (+ 1 2)
(force (delay (+ 1 2)))
;;=> 3
So that's worked. Then I go on defining `stream-cons', but this time there seems to be two ways:
(defmacro stream-cons (a b)
`(cons ,a ,(delay b)))
(defmacro stream-cons (a b)
`(cons ,a (delay ,b)))
I don't think they are different, but I am wrong! The first edition, which is a wrong edition, when called:
(force (cdr (stream-cons 'a (progn (print "hello") 2))))
;;=> (PROGN (PRINT "hello") 2)
(macroexpand '(stream-cons 'a (progn (print "hello") 2)))
;;=> (CONS 'A #<CLOSURE (LAMBDA # :IN STREAM-CONS) {25ABB3A5}>)
and the second edition, which is right, when called:
(force (cdr (stream-cons 'a (progn (print "hello") 2))))
;;
;; "hello"
;; => 2
(macroexpand '(stream-cons 'a (progn (print "hello") 2)))
;;=> (CONS 'A (DELAY (PROGN (PRINT "hello") 2)))
Now, I am very confused. Who can kindly help me to make clear the different of the two? Thanks a lot!
My Environment: Windows 32bits, SBCL 1.1.4