www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Some troubles with lazy lists

Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

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
Feb 28 2007