如果在Haskell中小于2,则删除列表中的前n个元素

时间:2013-10-12 00:35:01

标签: list haskell

如果小于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]

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。您也可以浏览takedrop等库函数。