我是 Haskell 的初学者,我正在尝试在列表末尾添加一个元素。
我输入一个像 [1,2,3,4] 这样的列表和一个数字 10。我想要一个像这样的输出 [1,2,3,4,10]。
My code:
func a [] = a
func a (x:xs) = x : func a (x:xs)
但我收到了这个错误:
Non type-variable argument in the constraint: Num [a]
(Use FlexibleContexts to permit this)
When checking that ‘it’ has the inferred type
it :: forall a. (Num a, Num [a]) => [a]
谁能帮忙解释一下吗?
一个简单的非递归方法怎么样:
func num xs = xs ++ [num]
或者如果你坚持递归:
func num [] = [num]
func num (x:xs) = x : func num xs
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)