[Haskell-cafe] Weaving fun

Bas van Dijk v.dijk.bas at gmail.com
Tue Apr 10 18:13:10 EDT 2007


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.)?

happy hacking,

Bas van Dijk

More information about the Haskell-Cafe mailing list