小编Era*_*ura的帖子

模式匹配并非详尽无遗

我正在尝试创建一个函数,该函数从整数列表中消除给定整数的倍数,格式为multiples x [y],其中 x 是给定的整数,y 是列表。

这是我所拥有的:

multiples :: Integer -> [Integer] -> [Integer]
multiples a [] = []
multiples a [b] = filter (\l -> l `mod` a /= 0) [b]
Run Code Online (Sandbox Code Playgroud)

multiples调用时会失败,说“函数倍数中的非穷尽模式”。所以我用ghci -Wall我的文件来查看缺少哪些模式,它返回这个:

multiples.hs:2:1: warning: [-Wincomplete-patterns]
    Pattern match(es) are non-exhaustive
    In an equation for `multiples': Patterns not matched: _ (_:_:_)

multiples.hs:2:11: warning: [-Wunused-matches]
    Defined but not used: `a'
Run Code Online (Sandbox Code Playgroud)

我觉得我在第 2 行中遗漏了一些非常简单的东西,但我有点卡住了。我究竟做错了什么?

haskell pattern-matching non-exhaustive-patterns

3
推荐指数
1
解决办法
3412
查看次数