如果小于2,则删除列表的前n个元素。错误说“因使用'print'而没有(showa0)的实例......”我从不理解Haskell错误按摩
func [] _ _ = []
func (x:xs) counter n
| x > 2 && counter < n = x :func xs counter limit
| otherwise = func xs (counter+1) limit
main = do
print $ func [3,1,4,2,1] 0 2
-- expectet output is [3,4,1]
答案 0 :(得分:1)
这应该删除编译错误:
func [] _ _ = []
func (x:xs) counter n
| x > 2 && counter < n = x :(func xs counter n )
| otherwise = func xs (counter+1) n
main = do
print $ func [3,1,4,2,1] 0 2
您已定义新变量limit
而非n
。您也可以浏览take
和drop
等库函数。