问题描述
所以,我的问题是,我必须编写一个程序来过滤列表中的所有 3 * x (3,6,9...) 个元素.我的程序看起来像:
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] []
编译器接受这一点,但在我输入时给出错误函数 noThirds 中的非详尽模式"noThirds [1,2,3,4,5,6,7,8]".我想这是因为我错过了各种help_ ..",但我不明白.我感谢每一个帮助!顺便说一句,不允许使用预定义的列表和算术函数.
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.
推荐答案
这是因为 noThirds
只有一个模式,[x]
只匹配单个元素列表.[x]
完全等同于 (x : [])
.我想你的意思是
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 中的非穷举模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!