0
我試圖在haskell中創建一個sum函數。我這樣做是爲了更熟悉這門語言。我定義它如下:Haskell - 瞭解何處和模式匹配
mysum :: [Integer] -> Integer
mysum lst = sm lst
where
sm :: [Integer] -> Integer
sm lst [] = 0
sm lst [x:xs]=
x + sm xs
這個想法是返回列表頭部的值,+尾部反饋到函數中。我記得在F#中做了類似的事情,但我不能讓它在haskell中工作。
The error im getting is:
sum.hs:5:5: error:
• Couldn't match expected type ‘Integer’
with actual type ‘[[Integer]] -> Integer’
• The equation(s) for ‘sm’ have two arguments,
but its type ‘[Integer] -> Integer’ has only one
In an equation for ‘mysum’:
mysum lst
= sm lst
where
sm :: [Integer] -> Integer
sm lst [] = 0
sm lst [x : xs] = x + sm xs
|
5 | sm lst [] = 0
| ^^^^^^^^^^^^^...