[英]Couldn't match expected type `Bool' with actual type `[[Integer]]
I'm new to Haskell and have a question about types, it error out because it couldn't match expected type.我是 Haskell 的新手,有一个关于类型的问题,它出错了,因为它无法匹配预期的类型。 I believe
squarePrime
is a bool, correct me if i'm wrong and it is complaining because it wants a int
?我相信
squarePrime
是一个布尔值,如果我错了,请纠正我,它正在抱怨,因为它想要一个int
? Can someone explain, thanks in advance.谁能解释一下,先谢谢了。
squarePrimes n = [n^2 | (n) <- [2..n-1], (all (\a -> mod n a /= 0) [2..n-1])]
multiOfFive a = mod a 5 == 0
fun n = [n | n <-[1..n], multiOfFive || squarePrimes n]
The 'squarePrimes n' function returns a list of Integers, as opposed to a Bool or a list of Bools. 'squarePrimes n' 函数返回整数列表,而不是 Bool 或 Bool 列表。 Looking at the list comprehension:
查看列表理解:
[n^2 | (n) <- [2..n-1], (all (\a -> mod n a /= 0) [2..n-1])]
This will produce a list of n^2, of n pulled from the range [2..n-1], for every n that satisfies the condition:对于满足条件的每个 n,这将生成一个 n^2 的列表,其中 n 个从范围 [2..n-1] 中提取:
(all (\a -> mod n a /= 0) [2..n-1])
Then, in the list comprehension used in the definition of 'fun':然后,在“乐趣”定义中使用的列表推导式中:
[n | n <-[1..n], multiOfFive || squarePrimes n]
A list is being constructed of each n in the range 1 to n, as long as the following condition is true:只要满足以下条件,就会构造一个由 1 到 n 范围内的每个 n 组成的列表:
multiOfFive || squarePrimes n
Haskell is expecting this to evaluate to a Bool. Haskell 期望它评估为 Bool。 However, 'multiOfFive' is being called without any argument, and 'squarePrimes n' returns a list of Integers, as opposed to a Bool.
然而,'multiOfFive' 是在没有任何参数的情况下调用的,并且 'squarePrimes n' 返回一个整数列表,而不是一个 Bool。
Without knowing exactly what the intention of 'fun n' is, I have altered the provided code a little bit to get a list comprehension that loads without error:在不知道“fun n”的确切意图的情况下,我对提供的代码进行了一些更改,以获得无错误加载的列表理解:
fun n = [n | n <- [1..n], (multiOfFive n) || (elem n (squarePrimes n))]
Now, it uses the 'elem' function to check whether a given 'n' is an element of the list 'squarePrimes n'.现在,它使用 'elem' 函数来检查给定的 'n' 是否是列表 'squarePrimes n' 的一个元素。
I believe
squarePrimes
is a Bool, correct me if I'm wrong我相信
squarePrimes
是一个布尔值,如果我错了,请纠正我
GHCi will tell you: GHCi 会告诉你:
λ> let squarePrimes n = [n^2 | (n) <- [2..n-1], (all (\a -> mod n a /= 0) [2..n-1])]
λ> :t squarePrimes
squarePrimes :: Integral a => a -> [a]
It is complaining because it wants an
int
?它抱怨是因为它想要一个
int
? Can someone explain有人能解释一下吗
Here Integral i => i
is any integer type.这里
Integral i => i
是任何整数类型。 That could for example be Int
or Integer
.例如,这可能是
Int
或Integer
。 This is because the integer literals 0
, 1
, 2
and the arithmetic operators you use aren't restricted to a specific integer type.这是因为整数文字
0
、 1
、 2
和您使用的算术运算符不限于特定的整数类型。
You're using squarePrimes n
as a Bool
.您正在使用
squarePrimes n
作为Bool
。
You're also using multiOfFive
as a Bool
when its type is:您还使用
multiOfFive
为Bool
时,它的类型是:
λ> let multiOfFive a = mod a 5 == 0
λ> :t multiOfFive
multiOfFive :: Integral a => a -> Bool
so a function that takes an integer type and returns a Bool
.所以一个接受整数类型并返回一个
Bool
的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.