<div dir="ltr">Hi Kurt,<br>
<br>
thank you for the detailed explanation. Indeed, I could follow your explanation on first reading :-)<br>
<br>
Dirk<br><br><div class="gmail_quote">2008/7/21 Kurt Hutchinson <<a href="mailto:kelanslists@gmail.com">kelanslists@gmail.com</a>>:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="Wj3C7c">On Mon, Jul 21, 2008 at 9:34 AM, Dirk Markert <<a href="mailto:dirk.markert@gmail.com">dirk.markert@gmail.com</a>> wrote:<br>
> I am trying to generate the following list:<br>
> 2, 3, 5 -- and then alternating (+2) resp (+4) -- 7, 11, 13, 17, 19, 23<br>
><br>
> I came up with the following solution<br>
> 2:3:unfoldr (\(a,b) -> Just (a,(a+b, if b == 2 then 4 else 2))) (5,2)<br>
><br>
> Are there easier ways to generate the desired list?<br>
<br>
<br>
</div></div>So you've got the beginnings of an infinite list, and a rule to modify<br>
that to generate new elements. How about turning that rule into a list<br>
of its own, and then combining them?<br>
<br>
rule = cycle [ 2, 4 ] -- this will give an infinite list of 2's and 4's<br>
<br>
Combining two lists is usually done with 'zip'. But in this case, we<br>
don't just want tuples, we want the sum of each pair. You can combine<br>
lists with a function by using 'zipWith'. Start at the point where the<br>
rule kicks in.<br>
<br>
rest = 5 : zipWith (+) rest rule<br>
<br>
Now just tack on your first elements.<br>
<br>
list = 2 : 3 : rest<br>
<br>
Here it is all in one place:<br>
<br>
list = 2 : 3 : rest<br>
where<br>
rule = cycle [ 2, 4 ]<br>
rest = 5 : zipWith (+) rest rule<br>
<font color="#888888"><br>
Kurt<br>
</font></blockquote></div><br></div>