[Haskell-cafe] Weaving fun
dfeustel at mindspring.com
Tue Apr 10 19:18:56 EDT 2007
Talk about synchronicity! I was just wondering whether 'weaving' of
infinite lists is possible.
eg weave the infinite lists [2,4..], [3,6..], [5,10..]
to get [2,3,4,5,6,8,9,10,..]
Is this kind of lazy evaluation possible?
>From: Bas van Dijk <v.dijk.bas at gmail.com>
>Sent: Apr 10, 2007 6:13 PM
>To: haskell-cafe at haskell.org
>Subject: [Haskell-cafe] Weaving fun
>For my own exercise I'm writing a function 'weave' that "weaves" a
>list of lists together. For example:
> weave [[1,1,1], [2,2,2], [3,3]] ==> [1,2,3,1,2,3,1,2]
> weave [[1,1,1], [2,2], [3,3,3]] ==> [1,2,3,1,2,3,1]
>Note that 'weave' stops when a list is empty. Right now I have:
> weave :: [[a]] -> [a]
> weave ll = work ll  
> work ll = foldr f (\rst acc -> work (reverse rst)  acc) ll
> f  g = \_ acc -> reverse acc
> f (x:xs) g = \rst acc -> g (xs:rst) (x:acc)
>However I find this definition hard to read and I'm questioning its
>efficiency especially due to the 'reverse' parts (how do they impact
>performance and can they be removed?)
>So I'm wondering if 'weave' can be defined more "elegantly" (better
>readable, shorter, more efficient, etc.)?
>Bas van Dijk
>Haskell-Cafe mailing list
>Haskell-Cafe at haskell.org
http://RepublicBroadcasting.org - Because You CAN Handle The Truth!
http://iceagenow.com - Because Global Warming Is A Scam!
More information about the Haskell-Cafe