[Haskell-beginners] Cyclic structures

Adrien Haxaire adrien at haxaire.org
Mon Apr 23 09:45:53 CEST 2012


 Hi,

 I do not have the answer to your question, but you might be interested 
 in the video about streams by Graham Hutton:

 http://channel9.msdn.com/Shows/Going+Deep/C9-Lectures-Graham-Hutton-How-To-Be-More-Productive

 Adrien

 On Mon, 23 Apr 2012 16:00:09 +1000, David Tchepak wrote:
> Im currently reading the first edition of "Introduction to Functional
> Programming" by Richard Bird and Philip Wadler. Section 7.6 is on
> cyclic structures, and has the following example:
>
>   ones = forever 1
>   forever x = x : forever x
>
> Which, after 5 evaluations, produces a graph like:
>
>   1 : 1 : 1 : 1 : 1 : forever 1
>
>  The book then has a re-written version of `forever` that looks like
> this:
>
>   forever x = zs
>     where zs = x : zs
>
> Which instead produces a cyclic structure:
>
>   * 1 : 
>   ^-----|
>
> So it takes up a fixed amount of space, despite being an infinite
> list.
>
> I was hoping someone could explain the difference between the first
> and second `forever`s for me. Does the second version become a cyclic
> graph because the `zs` intermediate expression has no additional
> arguments so doesnt need to be reduced further? Or do both end up
> being optimised the same by GHC? (my newbie attempts to profile show
> the second takes up a bit less space, but Im not sure if Im measuring
> it right).
>
> Appreciate any help.
> Regards,
> David

-- 
 Adrien Haxaire
 www.adrienhaxaire.org | @adrienhaxaire



More information about the Beginners mailing list