Monday, August 5, 2013

Streams from "Structure and Interpretation of Computer Programs" with Gambit Scheme

Figuring out how to implement Streams in the sense of Structure and Interpretation of Computer Programs by Abelson, Sussman and Sussman (great book!) in Gambit Scheme was difficult.

Clearly, "cons-stream" has to be a Macro.But how to do that with Gambit and it's confusing (for me!) macro system(s) ??

I found some older discussions and most helpful of all was

However, the definition of cons-stream works fine in the REPL, but when I put the code in a file "streams.scm" and load it via

(load "streams.scm")

I get an error:

1> (cons-stream 1 2)                                        
*** ERROR IN (console)@3.2 -- Unbound variable: cons-stream

The reason for this might be that Gambit needs to know about the macro before it is read by the reader. (?) "Including" the file with the macro definition did not work either. (confusing stuff for a Clojure programmer where Macros just work ... )

The solution is to put the Macro in a file ".gambcini".
Now, we are ready for stream-fun:

(cons-stream 1 2)  
(1 . #<promise #2>)

This works with Gambit v4.2.8 in the Debian Repositories and with Mac OSX.

No comments:

Post a Comment