[racket] Towers of Hanoi with Racket streams

From: Vlad Kozin (vladilen.kozin at gmail.com)
Date: Wed Oct 9 12:35:50 EDT 2013


Was going over Jay's Towers of Hanoi implementation that makes use of Racket streams. I find this comment confusing and would appreciate a more elaborate explanation:

> It’s important, using racket/stream, to use the stream-cons because if you wrote a stream-append with three arguments, then the second recursive call wouldn’t be lazily evaluated and you’d save nothing by using streams over lists. That’s because stream-append is a function, while stream-cons is a macro that thunks its arguments. 

Ok, so I assume what he means is that procedure arguments get evaled during application. Since (stream-append ..) is a procedure, the result of the second recursive call (last arg) would be forced. Docs for (stream-append ..) however say this:

> new stream is constructed lazily, while the last given stream is used in the tail of the result. 

I find this confusing. What does "costruct lazily" mean then? Does this phrase say that (n-1) args to it aren't forced, but the last one is? But append is a procedure, so all args should be evaled.

As a side thought, I think I could cheat (like stream-cons) by thunking stuff I pass to append, thunk is a wrapping macro, right?

> (stream-append (thunk (towers (sub1 n) from extra to))
>                (thunk (move from to))
>                (thunk (towers (sub1 n) extra to from)))

Vlad Kozin <vladilen.kozin at gmail.com>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131009/9351a526/attachment.html>

Posted on the users mailing list.