我是一個新望哈斯克爾,並有這段代碼:哈斯克爾錯誤:無法匹配類型「A」與「B」
import Control.Monad
data NestedList a = Elem a | List [NestedList a] deriving (Show)
instance Monad NestedList where
return a = List [Elem a]
(List (Elem a: xs)) >>= f = let a' = f a in a' `joinLists` xs
func :: a -> NestedList a
func a = List ([Elem a] ++ [Elem a])
joinLists :: NestedList a -> [NestedList a] -> NestedList a
joinLists (List a) b = List (a ++ b)
main = do let a = List [Elem 1, Elem 2] >>= func
print a
我所試圖做的是要接受列表使用元素,複製列表的第一個元素並向此列表添加尾部。因此,列表[Elem 1,Elem 2]將等於列表[Elem 1,Elem 1,Elem 2]。我知道這不是一個使用Monads的好例子,但那是爲了學習。
我得到這樣的錯誤:
Couldn't match type 'a' with 'b'
'a' is a rigid type variable bound by
the type signature for
'>>= :: NestedList a -> (a -> NestedList b) -> NestedList b
'b' is a rigid type variable bound by
the type signature for
'>>= :: NestedList a -> (a -> NestedList b) -> NestedList b
Expected type: [NestedList b]
Actual type: [NestedList a]
In the second argument of 'joinLists', namely 'xs'
我理解的錯誤,即它要求NestedList的不同類型的變量。這裏有什麼問題?
你的問題是使用'joinLists','a''是'ELEM B'但'xs'是'[Elem a]'。 monad綁定是'(>> =):: Monad m => ma - >(a - > mb) - > mb' – josejuan
@josejuan a'類型爲'NestedList a = List [Elem a]'且正確, 'xs'是'[Elem a]或[NestedList a]'。 joinLists需要NestedList a和[NestedList a],所以它不應該導致問題。我怎樣才能把'[Elem a]變成[Elem b]'? –
@AidasSimkus'f'的類型爲'(a - > m b)',這就是類型變量'b'的來源。 –