Hello everyone, I've found such code in the book "Real World Haskell", p68
data Tree a = Node a (Tree a) (Tree a)
| Empty
deriving (Show)
nodeAreSame (Node a _ _) (Node b _ _)
| a == b = Just a
nodeAreSame _ _ = Nothing
My question is: What job did the Just
data constructor do? When I delete it, I will get error message such like
(in ghci)
......
<Main *> nodeAreSame (Node 3 Empty Empty) (Node 3 Empty Empty))
<interactive>:1:16:
No instance for (Num (Maybe a))
......
But when I try to compare the type difference between "Just" and "No Just" version:
nodeAreSameJust :: (Eq t) => Tree t -> Tree t -> Maybe t
nodeAreSameNoJust :: (Eq a) => Tree (Maybe a) -> Tree (Maybe a) -> Maybe a
So what is the key point here? Does it means when I put a var with type a
in the node, the function will not output a node with type a
, so it get an error?