Generating cross recursive lazy sequences in clojure

44 views
Skip to first unread message

Christian Schuhegger

unread,
Mar 27, 2011, 4:28:28 AM3/27/11
to Clojure
Hi all,

I am continuing on my path to explore clojure in more detail and am
trying to implement the following haskell algorithm in clojure:
http://www.csse.monash.edu.au/~lloyd/tildeFP/Haskell/1998/Edit01/

Even after trying several different approaches and investing several
hours I do not seem to get a handle on the problem.

Clojure seems to be lazy, but not lazy enough for that algorithm,
because functions are always called eagerly. I tried combinations of
returning closures or using delays, but up to now none of my versions
are working.

Could anybody on the list give me a hint on how to approach that
problem?

Thanks,
Christian

Christian Schuhegger

unread,
Mar 27, 2011, 11:37:24 AM3/27/11
to Clojure
I understand now the problem. Clojure is really not lazy enough :) I
was forgetting that clojure evaluates its function arguments eagerly
like lisp and not lazily like haskell.

I have to wrap the function arguments that should be evaluated lazily
into closures "(fn [] value)".

Once I have a solution to the clojure implementation of the mentioned
algorithm I'll post it here.

Christian Schuhegger

unread,
Mar 27, 2011, 12:43:11 PM3/27/11
to Clojure
Finally! I have a solution. You can have a look at it here:
https://gist.github.com/889354/

I would like to hear comments about how to do it better or in a more
idiomatic clojure way.

Especially I am uncertain about my use of "binding" and the top level
declares. I needed the ability to reference the symbols from the
mutually recursive sequences and in addition the symbols needed to
have "dynamic extent".

Alan

unread,
Mar 28, 2011, 1:11:02 AM3/28/11
to Clojure
(1) lazy-cat is old. There's no reason to use it anymore; lazy-seq is
better for generating custom lazy seqs, and concat is already lazy.
(2) declare is overkill for this; just use letfn and avoid creating a
bunch of global functions that nobody else will ever use.

On Mar 27, 9:43 am, Christian Schuhegger

Meikel Brandmeyer

unread,
Mar 28, 2011, 2:03:14 AM3/28/11
to Clojure
Hi,

On 28 Mrz., 07:11, Alan <a...@malloys.org> wrote:

> (1) lazy-cat is old. There's no reason to use it anymore; lazy-seq is
> better for generating custom lazy seqs, and concat is already lazy.

You confuse this with lazy-cons. lazy-cat is just a really lazy
version of concat and there is no reason not to use it.

Sincerely
Meikel
Reply all
Reply to author
Forward
0 new messages