[Haskell-cafe] Weaving fun

Chris Mears chris at cmears.id.au
Tue Apr 10 19:26:55 EDT 2007


"Bas van Dijk" <v.dijk.bas at gmail.com> writes:

> 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]

[...]

> So I'm wondering if 'weave' can be defined more "elegantly" (better
> readable, shorter, more efficient, etc.)?

I don't know about your other criteria, but this is shorter:

weave [] = []
weave ([]:_) = []
weave ((x:xs):others) = x : weave (others ++ [xs])

It's also lazy:

> take 12 $ weave [[1..], [100..], [200..]]
[1,100,200,2,101,201,3,102,202,4,103,203]


More information about the Haskell-Cafe mailing list