在這裏工作是我的代碼:無法獲取類型簽名簡單的遞歸函數
test :: (Num a) => [a] -> a
test [] = 0
test [x:xs] = x + test xs
然而,當我運行它通過ghci中爲:l test
,我得到這個錯誤:
[1 1]編譯主要(test.hs,解釋)
test.hs:3:7:
Couldn't match type `a' with `[a]'
`a' is a rigid type variable bound by
the type signature for spew :: Num a => [a] -> a at test.hs:2:1
In the pattern: x : xs
In the pattern: [x : xs]
In an equation for `spew': spew [x : xs] = x + spew xs
Failed, modules loaded: none.
儘量不要笑:)這是我第一次嘗試在哈斯克爾。任何幫助或解釋都會很棒。 PS:我知道這可以通過摺疊很容易完成,但我正在嘗試寫我自己的類型簽名。提前致謝!!
Gah !!我應該看到的!謝謝! – 2013-04-23 21:51:54
@AnhanClark當我們學習Haskell時,我們都會經歷這種感覺。 – Dilawar 2013-04-23 23:36:53