
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? Thanks, Dave Feustel -----Original Message-----
From: Bas van Dijk
Sent: Apr 10, 2007 6:13 PM To: haskell-cafe@haskell.org Subject: [Haskell-cafe] Weaving fun Hello,
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 [] [] where 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.)?
happy hacking,
Bas van Dijk _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
http://RepublicBroadcasting.org - Because You CAN Handle The Truth! http://iceagenow.com - Because Global Warming Is A Scam!

This reminded me of "interleaving" as in:
Backtracking, Interleaving, and Terminating Monad Transformers
http://www.cs.rutgers.edu/~ccshan/logicprog/LogicT-icfp2005.pdf
On 4/10/07, Dave Feustel
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?
Thanks, Dave Feustel
-----Original Message-----
From: Bas van Dijk
Sent: Apr 10, 2007 6:13 PM To: haskell-cafe@haskell.org Subject: [Haskell-cafe] Weaving fun Hello,
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 [] [] where 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.)?
happy hacking,
Bas van Dijk _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
http://RepublicBroadcasting.org - Because You CAN Handle The Truth! http://iceagenow.com - Because Global Warming Is A Scam!
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Ricardo Guimarães Herrmann "You never change things by fighting the existing reality. To change something, build a new model that makes the existing model obsolete" - R. Buckminster Fuller

Dave Feustel:
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?
The base library version of (concat . transpose) can do that, since for infinite lists, you don't have the termination requirements of the OP. By the way, there is an error in my previous version of weave: *Main> weave [[1,1,1,1],[2,2],[3,3,3]] [1,2,3,1,2,3,1,1] Dan's version also has this behaviour. So, a correct list-based solution that doesn't use reverse or quadratic concatenation isn't immediately obvious. However, Chris Mears' solution can easily be adapted to use the O(1) snoc from Data.Sequence:
import Data.Sequence
weave = weaveSeq . fromList where weaveSeq xs = case viewl xs of (x:xs) :< xss -> x : weaveSeq (xss |> xs) _ -> []
participants (3)
-
Dave Feustel
-
Matthew Brecknell
-
Ricardo Herrmann