I am developing code from this document. It annoyed me that my translation was more detailed. It seems to me that I am missing something obvious that would make it as brief as the original Miranda.
Here is Miranda:
fix :: qtree(*) -> qtree(*) fix(Leaf(x)) = Leaf(x) fix(Internal(nw, ne, sw, se)) = merge(Internal(fix(nw), fix(ne), fix(sw), fix(se))) where merge(Internal (Leaf(x), Leaf(x), Leaf(x), Leaf(x))) = Leaf(x) merge(other) = other
Pay attention to the LHS merge . It captures the case when all four sheets have the same value. I cannot perform direct transliteration in Haskell, because I will receive a complaint about several x definitions. Here is my version:
fix :: (Eq a) => QTree a -> QTree a fix (Leaf a) = Leaf a fix (Internal nw ne sw se) = merge (Internal (fix nw) (fix ne) (fix sw) (fix se)) where merge internal@ (Internal (Leaf w) (Leaf x) (Leaf y) (Leaf z)) | (w == x) && (x == y) && (y == z) = Leaf x | otherwise = internal merge other = other
How can I get closer to what's going on in Miranda's code?
source share