[英]const function declaration in haskell
I am confused about one particular example of the const
function.我对
const
函数的一个特定示例感到困惑。 So the type declaration const :: a -> b->a
states that the function accepts two parameters of type a
and b
and returns a type a
.所以类型声明
const :: a -> b->a
声明该函数接受类型为a
和b
两个参数并返回类型a
。 For example:例如:
const 5 3 => 5
const 1 2 => 1
This makes sense based on the declaration.根据声明,这是有道理的。 However, I ran into this specific example:
但是,我遇到了这个特定的例子:
const (1+) 5 3 => 4
This makes me question my understanding of the function declaration.这让我质疑我对函数声明的理解。 I know this function only takes two parameters because I tried:
我知道这个函数只需要两个参数,因为我试过:
const 1 5 3
Now this reassures to me that it only takes 2 parameters.现在这让我放心,它只需要 2 个参数。 So how does this work?
那么这是如何工作的呢? Is the
(1+)
not a parameter? (1+)
不是参数吗? If not, what is it?如果不是,那是什么?
const (1+) 5 3 => 4
I know this function only takes two parameters (…)
我知道这个函数只需要两个参数(...)
Every function in Haskell takes one parameter. Haskell 中的每个函数都有一个参数。 Indeed, if you write:
事实上,如果你写:
const 5 1
then this is short for:那么这是缩写:
(const 5) 1
The type signature const :: a -> b -> a
is a more compact form of const :: a -> (b -> a)
.类型签名
const :: a -> b -> a
是const :: a -> (b -> a)
的更紧凑形式。
So const 5
will create a function that ignores the parameter (here 1
) and returns the value that it was given 5
.所以
const 5
将创建忽略的参数(这里的函数1
),并返回它被赋予了价值5
。
Now for const (1+) 5 3
thus thus means that we wrote:现在对于
const (1+) 5 3
因此意味着我们写道:
((const (1+)) 5) 3
const (1+)
will thus construct a function that ignores the parameter, and returns (1+)
, hence const (1+) 5
is (1+)
. const (1+)
将构造一个忽略参数的函数,并返回(1+)
,因此const (1+) 5
是(1+)
。 We thus then calculate:然后我们计算:
(1+) 3
which is 4
.这是
4
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.