I am learning about building and parsing binary trees in Haskell and I found a program example that I can't quite understand how works.. So the code is:
module Main where
data TTT aa = Leaf [aa] | Node (TTT aa) (TTT aa) deriving (Show,Eq);
a :: Num a => Integer -> Integer;
a x = x^2;
ff_a :: TTT Integer -> TTT Integer;
ff_a t = mm a t;
mm :: (Integer -> Integer) -> TTT Integer -> TTT Integer;
mm f (Node a1 a2) = Node(mm f a1) (mm f a2);
mm f (Leaf n) = Leaf(new_mm f n);
new_mm :: (Integer -> Integer) -> [Integer] -> [Integer];
new_mm f (a:a1) = f a : new_mm f a1;
new_mm f [] = [];
tree =
Node
(Node
(Node
((Leaf[16,-5,19]))
((Leaf[14,24,-55,27]))
)
(Node
((Leaf[37,11,64]))
((Leaf[-14,6,29]))
)
)
(Node
(Node
((Leaf[10,-19,22]))
((Leaf[3,4,5,-7]))
)
(Node
((Leaf[31,29,13]))
((Leaf[18,38,-4]))
)
)
main :: IO ();
t0 = tree
t1 = ff_a tree
main = do
print t0;
print t1;
Could someone explain to me how really functions mm and new_mm work? And what does Integer -> Integer and TTT integer -> TTT integer means and what data type is that?
new_mmis by definition equal to the function map. The type signature however is restricted toInteger.mmseems to be a variant of map, working for theTTTdefinition of binary trees (which have list leaves).data TTT aa = ...definition, I recommend to read the relevant section of Learn You a Haskell for Great Good.