-1

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

1 回答 1

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

于 2013-10-12T00:52:22.583 回答