4 ms·
As I mention below you can get the same result with lazy-sequences and it's very efficient.
by swannodette 16y ago
As I mention below you can get the same result with lazy-sequences and it's very efficient.
- ohyes 16y agoI think that you would have to be careful with that. To my knowledge, with lazy sequences in Clojure, the thunks are grouped into segments of 32. This chunking is for efficiency, but it has the (sometimes) weird implication that you ask for the first element in a sequence and it evaluates that first element, and the next 31 elements as well. This is troublesome if you attempt to use side effects. (Mr. Fogus has a nice writeup (and workaround) of this phenomena here: http://blog.fogus.me/2010/01/22/de-chunkifying-sequences-in-clojure/ http://blog.fogus.me/2010/01/22/de-chunkifying-sequences-in-...) I have to admit, I am not quite getting my head around how using a lazy sequence to the same effect would work (ignoring the chunking issue). I see how they are similar, a lazy sequence is just a flattened trampoline. Could you maybe pastebin or lisppaste an example? Anyway, I guess the point I was trying to make was not that it is impossible to do these things, but that TCO is a compiler level optimization of the function calling convention (manipulating the stack), and although Clojure has 'tail calls,' it can't really be called TCO in the true sense of the word. (Which is OK, as you said, there are other ways to do this sort of stuff).
- swannodette 16y agoYou can easily create true one-at-a-time lazy-sequences. I've been working on my own implementation of miniKanren (from The Reasoned Schemer) in Clojure. It's been quite challenging since the original implementation assumes the presence of TCO. Also one-at-time laziness is very much required here to enforce interleaving of streams results and to avoid divergence. I was happy to find I could get the same non-stack consuming behavior and that it turns out to perform better than miniKanren under Racket. https://github.com/swannodette/logos/blob/master/src/logos/minikanren.clj#L226 https://github.com/swannodette/logos/blob/master/src/logos/m...