Итак, я пытаюсь создать функцию для возврата двоичного дерева, которое является зеркалом первоначально отправленного двоичного дерева. Я думаю, что у меня есть это почти сделано, но я получаю странное «не мог соответствовать ожидаемый тип Вот мой код:.Haskell: зеркалирование двоичной ошибки дерева: «Не удалось сопоставить ожидаемый тип»
mirror :: BinTree a -> BinTree a
mirror (Node x tL tR) = Node x (mirror tR) (mirror tL)
А вот ошибка:
hw1.hs:84:30:
Couldn't match expected type `a' with actual type `BinTree a'
`a' is a rigid type variable bound by
the type signature for mirror :: BinTree a -> BinTree a
at hw1.hs:83:11
In the first argument of `Node', namely `x'
In the expression: Node x (mirror tR) (mirror tL)
In an equation for `mirror':
mirror (Node tL x tR) = Node x (mirror tR) (mirror tL)
hw1.hs:84:33:
Couldn't match expected type `a' with actual type `BinTree a'
`a' is a rigid type variable bound by
the type signature for mirror :: BinTree a -> BinTree a
at hw1.hs:83:11
In the return type of a call of `mirror'
In the second argument of `Node', namely `(mirror tR)'
In the expression: Node x (mirror tR) (mirror tL)
Failed, modules loaded: none.
It была моя вина. мое определение дерева отличается.
data BinTree a = Empty | Node (BinTree a) a (BinTree a) deriving (Eq,Show)
* новая функция су pposed быть: *
mirror :: BinTree a -> BinTree a
mirror Empty = Empty
mirror (Node tL x tR) = Node (mirror tR) x (mirror tL)
Можете ли вы опубликовать свое определение для 'BinTree a'? Ваш код компилируется для меня. – bheklilr
А сэр. Спасибо!! Я так новичок в haskell, что забыл проверить определение моего двоичного дерева ... Это был Node tL x tR, а не Node x tL tR ... Спасибо! –
Хорошо! Теперь, что произойдет, если вы будете «пустым»? –