Мое дерево определяется
data Tree a = Leaf a | Node (Tree a) (Tree a)
deriving (Show)
Я также объявляю дерево тестирования.
myTree = Node (Node (Leaf 1) (Leaf 2)) (Leaf 3)
Я хочу создать функцию maptree f, которая будет работать на Leaf. А точнее, f x = x +1
,
тогда maptree f myTree
вернется
Node (Node (Leaf 2) (Leaf 3)) (Leaf 4)
Мое решение
maptree f (Leaf a)= Leaf (f a)
maptree f (Node xl xr ) = Node (maptree xl) (maptree xr)
но вернется следующая ошибка
Couldn't match expected type `Tree a'
against inferred type `Tree t -> Tree t'
Probable cause: `maptree' is applied to too few arguments
In the first argument of `Node', namely `(maptree xl)'
In the expression: Node (maptree xl) (maptree xr)
Сбой, загруженные модули: нет.
Однако, если я сделаю
maptree (Leaf a)= Leaf ( a + 1)
maptree (Node xl xr ) = Node (maptree xl) (maptree xr)
это работает.
Я не вижу разницы между первой функцией и второй. Как я могу получить ошибку? Спасибо.