[Haskell-cafe] Code Golf

Sebastian Fischer sebf at informatik.uni-kiel.de
Wed Apr 15 07:28:56 EDT 2009


> Prelude> let diag = concat . diags where diags ((x:xs):xss) = [x] :  
> zipWith (:) xs (diags xss)

this has a different semantics on finite lists, so I should add a test  
case:

*Main> diag [[1,2,3],[4,5,6],[7,8,9]]
[1,2,4,3,5,7,6,8,9]

Your version yields [1,2,4,3,5,7].

Actually, there are a number of implementations that implement the  
same behaviour as the original version, e.g.,

   diag = concat . foldr diags []
    where  diags []         ys       = ys
           diags (x:xs)     ys       = [x] : merge xs ys

           merge []         ys       = ys
           merge xs@(_:_)   []       = map (:[]) xs
           merge (x:xs)     (y:ys)   = (x:y) : merge xs ys

I'd be interested if one can *derive* from the original version a  
simpler version using clever pointfree combinators.

Cheers,
Sebastian


More information about the Haskell-Cafe mailing list