在这里工作是我的代码:无法获取类型签名简单的递归函数
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