[Haskell-cafe] Weaving fun

Ricardo Herrmann rherrmann at gmail.com
Tue Apr 10 19:24:55 EDT 2007


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 <dfeustel at mindspring.com> wrote:
>
> 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 <v.dijk.bas at gmail.com>
> >Sent: Apr 10, 2007 6:13 PM
> >To: haskell-cafe at 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 at 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 at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070410/73858a83/attachment.htm


More information about the Haskell-Cafe mailing list