Bernie Pope bjpop at csse.unimelb.edu.au
Mon Jul 21 10:21:22 EDT 2008

```On 21/07/2008, at 11:34 PM, Dirk Markert wrote:

> I am trying to generate the following list:
> 2, 3, 5 -- and then alternating (+2) resp (+4) -- 7, 11, 13, 17, 19,
> 23
>
> I came up with the following solution
> 2:3:unfoldr (\(a,b) -> Just (a,(a+b, if b == 2 then 4 else 2))) (5,2)
>
> Are there easier ways to generate the desired list?

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.

```