[Haskell-cafe] Weaving fun

Brandon Michael Moore brandon at heave.ugcs.caltech.edu
Tue Apr 10 19:04:02 EDT 2007


On Wed, Apr 11, 2007 at 12:13:10AM +0200, Bas van Dijk wrote:
> 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:

If it wasn't for that, you could use

import Data.List(transpose)
weave :: [[a]] -> [a]
weave = concat . transpose

e.g.
>  weave [[1,1,1], [2,2], [3,3,3]] ==> [1,2,3,1,2,3,1,3]

Brandon 


More information about the Haskell-Cafe mailing list