[Haskell-begin] Alternating sequence

Dirk Markert dirk.markert at gmail.com
Mon Jul 21 11:52:05 EDT 2008


Hi Bernie,

I like your much more. I tried also to find a solution using cycle but
didn't succeed.

Thank you,
Dirk

2008/7/21 Bernie Pope <bjpop at csse.unimelb.edu.au>:

> Hi Dirk,
>
> I doubt this is "easier", but it is a nice opportunity to show off some
> cyclic programming:
>
>   suffix = 5 : [ x + y | (x,y) <- zip suffix (cycle [2,4])]
>   list = 2 : 3 : suffix
>
> cycle is from the Prelude and is used above to build an "infinite" list of
> [2,4,2,4 ...]
>
> Note that suffix is recursive. Lazy evaluation allows us to pull values out
> of it whilst building it at the same time. Obviously it is important that we
> start building the list before trying to pull any values out of it, hence
> the "5 :" at the start is important.
>
> I may not have answered your question, but hopefully it is interesting
> nonetheless.
>
> Cheers,
> Bernie.
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20080721/12a4c1e3/attachment.htm


More information about the Beginners mailing list