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 ;)<br>
<br>ghci> let diag = foldr (curry (prod mappend fst snd . uncurry (coprod mappend (splitAt 2) (splitAt 1)))) []<br><br>ghci> diag [[1,2,3],[4,5,6],[7,8,9]]<br>[1,2,4,3,5,7,6,8,9]<br>ghci> diag [[1,2,3],[4],[5,6,7]]<br>
[1,2,4,3,5,6,7]<br><br><br><div class="gmail_quote">On Wed, Apr 15, 2009 at 5:32 AM, Sebastian Fischer <span dir="ltr"><<a href="mailto:sebf@informatik.uni-kiel.de">sebf@informatik.uni-kiel.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Fancy some Codegolf?<br>
<br>
I wrote the following function for list diagonalization:<br>
<br>
> diag l = foldr (.) id ((sel l . flip sel) ((:[]).(:))) []<br>
> where<br>
> sel = foldr (\a b c -> id : mrg (a c) (b c)) (const []) . map (flip id)<br>
><br>
> mrg [] ys = ys<br>
> mrg xs [] = xs<br>
> mrg (x:xs) (y:ys) = (x.y) : mrg xs ys<br>
<br>
Self explanatory, isn't it? Here is a test case:<br>
<br>
*Main> take 10 $ diag [[ (m,n) | n <- [1..]] | m <- [1..]]<br>
[(1,1),(1,2),(2,1),(1,3),(2,2),(3,1),(1,4),(2,3),(3,2),(4,1)]<br>
<br>
I was trying to golf it down [^1] but my brain explodes. If you succeed in reducing keystrokes, I'd be happy to know!<br>
<br>
Cheers,<br>
Sebastian<br>
<br>
[^1]: <a href="http://codegolf.com/" target="_blank">http://codegolf.com/</a><br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br>