Haskell有什么样的'约束'意思 [英] What does has kind 'Constraint' mean in Haskell
问题描述
我试图做的功能是解决任意维数向量的毕达哥拉斯定理。这看起来像这样:平方根(a ^ 2 + b ^ 2 + c ^ 2 ...)
我可以在ghci中做几行操作,我试图做一个函数如下:
pre $ code sq x = x * x
b = map sq [1,2,3]
a = sum b
x = sqrt b
当我这样做时,我试图包含许多类型的签名,
目前我的函数如下所示:
mod :: [Num a] => a
mod x = sqrt a
其中a = sum [b]
其中[b] = map sq [x]
当我尝试运行它时,我不明白这个问题:
预期约束,但'[Num a]'有'*'
•在类型签名中:
Main.mod :: [Num a] => a
几件事要调整:
0) mod
对您的函数来说不是一个好名字,因为它是标准库中模函数的名称。我将它称为 norm
。
1)您打算编写的类型签名是:
norm :: Num a => [a] - > a
[a]
是一个元素类型为 a
的列表。 =>
之前的 Num a
不是类型,而是约束 ,它指定 a
必须是一个数字类型(或者更准确的说,它必须是 Num $ c $的一个实例c>类)。
[Num a] =>
会导致您所看到的错误,因为在给定方括号的情况下,类型检查器将其视为尝试使用列表类型而不是约束。
除了 Num a
问题之外,您从签名中省略了结果类型。校正后的签名反映出你的函数需要一个数字列表并返回一个数字。
<2>
Num a
对于你想要做的事情,约束太弱。为了使用 sqrt
,您不仅需要一个数字类型,而且还需要一个浮动
(参考左边的评论对这个答案): GHCi> :t sqrt
sqrt :: Floating a => a - > a
因此,您的签名应该是
norm :: Floating a => [a] - > a
<3> [x]
是一个列表中包含一个元素 x
。如果你的参数已经是一个列表,就像类型签名所说的那样,不需要把它放在方括号中。然后你的函数变成:
norm :: Floating a => [a] - > a
标准x = sqrt a
其中a =总和b
其中b =地图平方x
或者,更整齐地说,没有第二个其中
-block:
norm :: Floating a => [a] - > a
norm x = sqrt(b)
其中b = map sq x
I am fresh to Haskell and I am trying to understand the language by writing some code. I am only familiar with very simple instructions on ghci: head, tail, sum, (*), and the like – very simple.
The function I am trying to make is for solving Pythagoras's theorem for vectors of any number of dimensions. This looks something like this: square root (a^2 + b^2 + c^2 ...)
What I can do in ghci in a few lines, which I am trying to make a function is the following:
sq x = x*x
b = map sq [1,2,3]
a = sum b
x = sqrt b
When I do this I try to include a signature of many sorts, Currently my function looks like this:
mod :: [Num a] => a
mod x = sqrt a
where a = sum [b]
where [b] = map sq [x]
I do not understand the issue when I try to run it:
Expected a constraint, but ‘[Num a]’ has kind ‘*’
• In the type signature:
Main.mod :: [Num a] => a
A few things to adjust:
0) mod
isn't a good name for your function, as it is the name of the modulo function from the standard library. I will call it norm
instead.
1) The type signature you meant to write is:
norm :: Num a => [a] -> a
[a]
is the type of a list with elements of type a
. The Num a
before the =>
isn't a type, but a constraint, which specifies that a
must be a number type (or, more accurately, that it has to be an instance of the Num
class). [Num a] =>
leads to the error you have seen because, given the square brackets, the type checker takes it as an attempt to use a list type instead of a constraint.
Beyond the Num a
issue, you have left out the result type from the signature. The corrected signature reflects that your function takes a list of numbers and returns a number.
2) The Num a
constraint is too weak for what you are trying to do. In order to use sqrt
, you need to have not merely a number type, but one that is an instance of Floating
(cf. leftaroundabout's comment to this answer):
GHCi> :t sqrt
sqrt :: Floating a => a -> a
Therefore, your signature should be
norm :: Floating a => [a] -> a
3) [x]
is a list with a single element, x
. If your argument is already a list, as the type signature says, there is no need to enclose it in square brackets. Your function, then, becomes:
norm :: Floating a => [a] -> a
norm x = sqrt a
where a = sum b
where b = map sq x
Or, more neatly, without the second where
-block:
norm :: Floating a => [a] -> a
norm x = sqrt (sum b)
where b = map sq x
这篇关于Haskell有什么样的'约束'意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!