[Haskell-cafe] Code Golf

Miguel Mitrofanov miguelimo38 at yandex.ru
Wed Apr 15 07:42:57 EDT 2009


What about

diag [[1,2,3],[4],[5,6,7]]

?

What it should be?

Sebastian Fischer wrote on 15.04.2009 15:28:
>> 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
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 


More information about the Haskell-Cafe mailing list