cây của tôi được xác định bởiHaskell Bản đồ cho Trees
data Tree a = Leaf a | Node (Tree a) (Tree a)
deriving (Show)
Tôi cũng tuyên bố một cây thử nghiệm.
myTree = Node (Node (Leaf 1) (Leaf 2)) (Leaf 3)
Điều tôi muốn làm là tạo một hàm maptree f sẽ hoạt động trên Leaf. Để cụ thể hơn, f x = x +1
,
sau đó maptree f myTree
sẽ trở lại
Node (Node (Leaf 2) (Leaf 3)) (Leaf 4)
Giải pháp của tôi là
maptree f (Leaf a)= Leaf (f a)
maptree f (Node xl xr) = Node (maptree xl) (maptree xr)
nhưng nó sẽ trở lại các lỗi sau
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)
Không, mô-đun được nạp : không ai.
Tuy nhiên, nếu tôi làm
maptree (Leaf a)= Leaf (a + 1)
maptree (Node xl xr) = Node (maptree xl) (maptree xr)
nó làm việc ra.
Tôi không thể thấy sự khác biệt giữa hàm đầu tiên và hàm thứ hai. Làm cách nào để tôi gặp lỗi? Cảm ơn.
Tôi hiện đã hoạt động. Tôi là ngu ngốc ...>< –
Nên là maptree f (Node xl xr) = Node (maptree f xl) (maptree f xr) thay vì maptree f (Node xl xr) = Node (maptree xl) (maptree xr) –