use of make-lazy?

23 views
Skip to first unread message

peter

unread,
Nov 12, 2010, 9:48:51 AM11/12/10
to Land of Lisp
Good afternoon all,

I'm struggling a bit with the purpose of converting a plain list into
a lazy list.

Isn't the purpose of a lazy list to avoid having to store large
amounts of data, and instead providing a way to calculate the next
element when needed? The *integers* and the moves are clear examples
of this.

But make-lazy? It stores all data of the original list, plus the
overhead of the lazy-cons cells. So what's the gain here?

Confused greetings,
Peter.

Jason Stidd

unread,
Nov 14, 2010, 1:35:29 PM11/14/10
to Land of Lisp
From the point of view of Haskell, the benefit of the lazy list is not
to shorten the list, but to only calculate what you need. The list may
be large (in fact you can use infinite lists with lazy evaluation),
but if you only evaluate or look at what you need, its faster. If I
have a list of 1000 items and I only need the car or the first five
items for instance, there's no reason to look at all 1,000 items.
Especially if I'm going to be looking at the list three or four times.
And in addition, it won't be evaluated until we absolutely need it.
This is a huge benefit if for some reason we don't end up needing to
evaluate it all. I hope this helps.

~jason

Conrad

unread,
Nov 14, 2010, 4:05:02 PM11/14/10
to Land of Lisp
The purpose of putting things into a lazy list is that it's convenient
to break some kinds of calculations into two steps:
1. Build a data structure to help perform the calculation
2. Use the data structure to calculate.

However, this approach is impractical if the data structure ends up
being too large, or requires too much time to generate.

Sometimes, however, step 2 (where we look at the data structure to do
calculations) only needs to look at a small subset of all of the data
therein. By using lazy lists to create the intermediate data
structure, only a small fraction of the data structure will ever
actually exists "for real" because we only look at that small fraction
of the data in step 2.

This is what were doing in the board game example, where our
calculations are basically:
1. Generate a tree of every possible move and future move in the
game with lazy lists.
2. Look at _some_ of these possible moves to calculate a good
move.

Even though the data generated by step 1 may be prohibitively large,
we actually only calculate possible moves on an "as needed" basis.
This would only be possible by performing the calculations in step 1
in a lazy manner.

peter

unread,
Nov 14, 2010, 5:47:11 PM11/14/10
to Land of Lisp
Hi Jason, Conrad

On Nov 14, 7:35 pm, Jason Stidd <stiddsg...@gmail.com> wrote:
> From the point of view of Haskell, the benefit of the lazy list is not
> to shorten the list, but to only calculate what you need. The list may
> be large (in fact you can use infinite lists with lazy evaluation),
> but if you only evaluate or look at what you need, its faster. If I
> have a list of 1000 items and I only need the car or the first five
> items for instance, there's no reason to look at all 1,000 items.
> Especially if I'm going to be looking at the list three or four times.
> And in addition, it won't be evaluated until we absolutely need it.
> This is a huge benefit if for some reason we don't end up needing to
> evaluate it all.  I hope this helps.

I'm not familiar with Haskell, but I'm sure it cannot have an infinite
lazy list with all those items actually present. And even with non-
lazy lists, if you only need the first 5 items, you don't have to
look at all 1000 items either (unless you want the last 5 items -- but
then a lazy list would not be better off).

I think this is in line with Conrad's reply to my question, where he
states that

> Even though the data generated by step 1 may be prohibitively large,
> we actually only calculate possible moves on an "as needed" basis.
> This would only be possible by performing the calculations in step 1
> in a lazy manner.

But my original question remains: what is the use of MAKE-LAZY? It
converts an *existing* list, with *all data present*, into a lazy
list, a system which is designed to only generate the data needed.

Note that I'm not questioning the purpose of lazy lists; I'm
questioning the usefulness of MAKE-LAZY (other than educational).

Cheers,
Peter.

Conrad

unread,
Nov 15, 2010, 9:47:14 AM11/15/10
to Land of Lisp
You are right- I didn't read your question carefully enough.

Indeed, there are few uses for MAKE-LAZY. It's main use is as a
debugging tool- Or you might use it in a test suite.

Jason Stidd

unread,
Nov 15, 2010, 2:39:07 PM11/15/10
to Land of Lisp
Land of Lisp: Pg. 382 ".....lazy lists can be infinite." However, I
apologize that my answer was not helpful. Next time I'll keep quiet.

peter

unread,
Nov 15, 2010, 3:52:43 PM11/15/10
to Land of Lisp
Hi Jason,

On Nov 15, 8:39 pm, Jason Stidd <stiddsg...@gmail.com> wrote:
> Land of Lisp: Pg. 382 ".....lazy lists can be infinite." However, I
> apologize that my answer was not helpful. Next time I'll keep quiet.

Please don't. Keep quiet, I mean. And you really don't have to
apologize. If I had to apologize for every unhelpful answer I gave or
silly question I asked I wouldn't be able to do anything else...

That being said, I apologize for giving you the impression that you
needed to apologize...

And now, back to page 419!
Peter.
Reply all
Reply to author
Forward
0 new messages