或接受两个值参数haskell



我是haskell的初学者,我试图为埃拉托色尼的筛定义一个简单的函数,但它说错误:

• Couldn't match expected type ‘Bool -> Bool’
with actual type ‘Bool’
• The function ‘or’ is applied to two value arguments,
but its type ‘t Bool -> Bool’ has only one
In the expression: or (mod n x) (divl l x)
In an equation for ‘divl’: divl (n : l) x = or (mod n x) (divl l x)
|
13 | divl (n:l) x = or (mod n x) (divl l x)
erat l [] = l
erat l (x:t) = if divl l x then erat l t else erat (x:t) l
divl (n:l) x = or (mod n x) (divl l x)
divl [] x = True

我试着用" ' "但是没有任何效果

or不是布尔或运算符;这是(||)or接受一个布尔值列表(好吧,Foldable),如果至少有一个值是True,则返回True

> or []
False
> or [True, False]
True

(注意,or []被定义为False,以保持or xs || or ys == or (xs ++ ys)的标识。作为具体的例子,or [] || or [False] == or [False].)

尝试使用(||) :: Bool -> Bool -> Bool代替or :: Foldable t => t Bool -> Bool

https://hackage.haskell.org/package/base-4.17.0.0/docs/Prelude.html v: -124 - 124,

https://hackage.haskell.org/package/base-4.17.0.0/docs/Prelude.html v:或

mod n x || divl l xor [mod n x, divl l x].

您需要检查模是否为零,或者剩余的一个,您使用(||) :: Bool -> Bool -> Bool:

divl (n:l) x = mod n x == 0 || divl l x
divl [] x = False

基例应该是False,否则它总是返回True。但是,使用any可能更好:

divl ls x = any ((0 ==) . (`mod` x)) ls

最后总结了我解决的问题

erat :: [Int] -> [Int] -> [Int]
erat l [] = l
erat l (x:t) = if divl l x then erat l t else erat (x:l) t
divl (n:l) x = ((mod x n) == 0) || (divl l x)
divl [] x = False

相关内容

  • 没有找到相关文章

最新更新