[Haskell-cafe] Fair diagonals (code golf)

Sjoerd Visscher sjoerd at w3future.com
Wed Nov 4 13:01:50 EST 2009


The code by Twan can be reduced to this:

diagN = concat . foldr f [[[]]]

f :: [a] -> [[[a]]] -> [[[a]]]
f xs ys = foldr (g ys) [] xs

g :: [[[a]]] -> a -> [[[a]]] -> [[[a]]]
g ys x xs = merge (map (map (x:)) ys) ([] : xs)

merge :: [[a]] -> [[a]] -> [[a]]
merge [] ys = ys
merge xs [] = xs
merge (x:xs) (y:ys) = (x++y) : merge xs ys

But my feeling is that this can still be simplified further. Or at  
least refactored so it is clear what actually is going on!

--
Sjoerd Visscher
sjoerd at w3future.com





More information about the Haskell-Cafe mailing list