1

In the book(Structure and Interpretation of Computer Programs section 3.52, Chinese Version) ,we use code:

(define ones (cons-stream 1 ones))

to define a infinite stream of one.

But when I ran this, I got a Unbound variable: ones error. Why? Did I miss something?

The part of English version of the book

Rahn
  • 4,787
  • 4
  • 31
  • 57
midpush
  • 27
  • 8
  • Hard to tell without more details (what book? what interpreter?), but I'd suggest you try using a different language or fiddle around with your interpreter's configuration, something is restricting references to undefined variables. – Óscar López Jan 18 '16 at 15:08
  • The book is the Chinese version of *Structure and Interpretation of Computer Programs*. I just readed the English Version and upload the picture of the part of the book. I don't think it's because of the bad translation.@ÓscarLópez – midpush Jan 19 '16 at 02:57
  • Can you explain exactly what you did when you "ran this"? Did you enter the definition in the REPL? Did you load a file with the definition? Did you try to print `ones` after defining it? Something else? – molbdnilo Jan 19 '16 at 08:02
  • You are right. I defined "cons-stream" as a lambda before I ran this. Thank you. – midpush Jan 19 '16 at 08:22
  • you know, you can mentally "inline" that macro, and just write `(define ones (lambda () (cons 1 ones)))`. – Will Ness Jan 19 '16 at 09:26
  • btw you need to undelete your answer and accept it, so all can see that the issue has been resolved. :) – Will Ness Jan 19 '16 at 09:36
  • `cons-stream` might need to be defined as a macro. E.g., see http://stackoverflow.com/questions/14640833/how-is-the-sicp-cons-stream-implemented. – Joshua Taylor Jan 19 '16 at 22:30
  • Also see: [Why is it legal in a function definition to make self-call but illegal for a value?](http://stackoverflow.com/questions/31896117/why-is-it-legal-in-a-function-definition-to-make-self-call-but-illegal-for-a-val?lq=1) – Joshua Taylor Jan 19 '16 at 22:33

1 Answers1

1

I defined "cons-stream" as a lambda before I ran this. But "cons-stream" need to be a macro. :)

midpush
  • 27
  • 8