问题描述
所以,我的问题是,我必须编写一个程序来过滤列表中的所有3 * x(3,6,9 ...)元素。我的程序看起来像: length':: [a] - > Int
length'= foldr(\_ - >(+1))0
help_ :: [a] - > [a] - > [a]
help_(x:xs)[] = help_(xs)[x]
help_ [](x)=(x)
help_(x:xs) (y)
|((length'[xs])== 0)=(y)
|((length'[y])`mod` 2 == 0)= help_(xs)
|否则= help_(xs)(y ++ [x])
noThirds :: [a] - > [a]
noThirds [x] = help_ [x] []
但是当我输入
noThirds [1,2,3,4,5,6,7,8]
时会出现错误函数noThirds中的非穷举模式。我想这是因为我缺少各种help_ ..,但我不明白。感谢每一个帮助!
btw预定义列表和算术函数是不允许的。
这是因为 noThirds
只有一个模式,
[x]
,它只能匹配单个元素列表。
[x]
完全等价于(x:[])
。我认为你的意思是
noThirds :: [a] - > [a]
noThirda xs = help_ xs []
So, my Problem is, that I have to write a program that filters all 3 * x (3,6,9...) elements from a list. My program looks like:
length' :: [a] -> Int
length' = foldr (\_ -> (+1)) 0
help_ :: [a] -> [a] -> [a]
help_ (x:xs) [] = help_ (xs) [x]
help_ [] (x) = (x)
help_ (x:xs) (y)
|((length' [xs]) ==0) = (y)
|((length' [y]) `mod` 2 ==0) = help_ (xs) (y)
|otherwise = help_ (xs) (y++[x])
noThirds :: [a] -> [a]
noThirds [x] = help_ [x] []
The compiler accepts this but gives the error "Non exhaustive pattern in function noThirds" when I enter "noThirds [1,2,3,4,5,6,7,8]". I guess it's cause im missing a variety of "help_ .." but I don't get it. Im grateful for every help!Btw predefined list and arithmetic functions are not allowed.
It's because noThirds
only has one pattern, [x]
which only matches against a single element list.[x]
is exactly equivalent to (x : [])
. What I think you meant was
noThirds :: [a] -> [a]
noThirda xs = help_ xs []
这篇关于功能noThirds中没有详尽的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!