【发布时间】:2026-02-22 07:40:01
【问题描述】:
我正在尝试创建一个函数,以 multiples x [y] 的形式从整数列表中消除给定整数的倍数,其中 x 是给定整数,y 是列表。
这是我所拥有的:
multiples :: Integer -> [Integer] -> [Integer]
multiples a [] = []
multiples a [b] = filter (\l -> l `mod` a /= 0) [b]
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'
我觉得我在第 2 行中遗漏了一些非常简单的东西,但我有点卡住了。我做错了什么?
【问题讨论】:
标签: haskell pattern-matching non-exhaustive-patterns