के लिए मेरे पेड़ सेहास्केल मानचित्र पेड़
data Tree a = Leaf a | Node (Tree a) (Tree a)
deriving (Show)
परिभाषित किया गया है मैं भी एक परीक्षण पेड़ की घोषणा।
myTree = Node (Node (Leaf 1) (Leaf 2)) (Leaf 3)
मैं जो करना चाहता हूं वह एक समारोह मैपट्री एफ बनाता है जो पत्ता पर कार्य करेगा। अधिक विशेष रूप से होने के लिए, 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)
है।
मैं पहले फ़ंक्शन और दूसरे के बीच का अंतर नहीं देख सकता। मुझे त्रुटि कैसे मिलती है? धन्यवाद।
मुझे अब यह काम मिल गया है। मैं बेवकूफ हूँ ...>< –
maptree f (नोड xl xr) = node (maptree f xl) (maptree f xr) maptree f (नोड xl xr) = नोड (maptree xl) के बजाय होना चाहिए (maptree xr) –