[Haskell-cafe] Code Golf

Matt Morrow moonpatio at gmail.com
Wed Apr 15 22:59:29 EDT 2009


I think this has the semantics you're looking for. (it would probably be
somewhat prettier if "mappend" wasn't such an ugly identifier (compared to,
say, (++)), but this is just me trying to sneak a shot in against the Monoid
method's names ;)

ghci> let diag = foldr (curry (prod mappend fst snd . uncurry (coprod
mappend (splitAt 2) (splitAt 1)))) []

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


On Wed, Apr 15, 2009 at 5:32 AM, Sebastian Fischer <
sebf at informatik.uni-kiel.de> wrote:

> Fancy some Codegolf?
>
> I wrote the following function for list diagonalization:
>
> > diag l = foldr (.) id ((sel l . flip sel) ((:[]).(:))) []
> >  where
> >   sel = foldr (\a b c -> id : mrg (a c) (b c)) (const []) . map (flip id)
> >
> >   mrg []     ys     = ys
> >   mrg xs     []     = xs
> >   mrg (x:xs) (y:ys) = (x.y) : mrg xs ys
>
> Self explanatory, isn't it? Here is a test case:
>
>    *Main> take 10 $ diag [[ (m,n) | n <- [1..]] | m <- [1..]]
>    [(1,1),(1,2),(2,1),(1,3),(2,2),(3,1),(1,4),(2,3),(3,2),(4,1)]
>
> I was trying to golf it down [^1] but my brain explodes. If you succeed in
> reducing keystrokes, I'd be happy to know!
>
> Cheers,
> Sebastian
>
> [^1]: http://codegolf.com/
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090415/f6935c28/attachment.htm


More information about the Haskell-Cafe mailing list