Some troubles with lazy lists
Reiner Pope
reIGNOREiner.poCAPSpe at gmFOOail.cBARom
Wed Feb 28 21:59:36 PST 2007
Hi all,
I've been trying to implement lazy lists in D, to see how 'lazy' can be
useful (code is attached). I've run into several difficulties:
My implementation of the lazy list has this constructor:
> this(T head, List!(T) delegate() tail)
and I want to call it with the 'lazy' syntactic sugar (which would avoid
the need for writing '{return ones;}' as opposed to just 'ones').
However, I wanted to _store_ the delegate, and I couldn't find any way
to do that with 'lazy List!(T)', because every time I tried to access
it, it evaluated the function. What I ended up doing was wrapping it in
> List!(T) Cons(T)(T head, List!(T) delegate()[] tail...)
> in
> {
> assert(tail.length == 1);
> }
> body
> {
> return new ConsList!(T)(head, tail[0]);
> }
I also can't do the following
> // not allowed: (undefined identifier ones)
> // auto ones = Cons(1, ones);
> List!(int) ones; ones = Cons(1, ones);
This 'infinite list' is allowed because the second parameter is
evaluated lazily. However, DMD doesn't like me using 'ones' in the same
line I define it, so I need to add this semicolon. I consider this
messier, because it disallows type inference. Can anything be done about
this?
I would guess that both of these problems can't easily be solved, but
I've thought this in the past and been pleasantly surprised, so maybe
someone else can suggest something better?
Thanks,
Reiner
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: lists.d
Url: http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20070301/2f0366d9/attachment.txt
More information about the Digitalmars-d-learn
mailing list