I have this type, which defines an expression. I know that *
symbol lets me add pairs, but what is the ->
for?
我有这种类型,它定义了一个表达式。我知道*符号可以让我添加对,但是什么是 - > for?
# type expression = Value of float
| Sum of (expr*expr)
| Subtraction of (expr*expr)
| Fc1 of ((float->float)*expr)
1 个解决方案
#1
The ->
operator is for function types. a -> b
means "a
in, b
out", so float -> float
is the type of functions that take a float as their argument and produce a float as their result.
- >运算符用于函数类型。 a - > b表示“a in,b out”,所以float - > float是一个函数类型,它将float作为参数并生成一个float作为结果。
What about
float -> float -> float
那浮点数 - >浮点数 - >浮点数
->
is right-associative, so a -> b -> c
is the same as a -> (b -> c)
meaning a function that takes an a
and produces another function of type b -> c
. Functions like this are often used to simulate multi-arguments functions (you can use f x y
to apply f
to x
and then apply the resulting function to y
, which effectively calls the inner function with two arguments) as an alternative to tuples. This way of simulating multi-argument functions is called currying.
- >是右关联的,所以a - > b - > c与 - >(b - > c)相同,这意味着一个函数接受a并产生另一个类型为b - > c的函数。像这样的函数通常用于模拟多参数函数(你可以使用f x y将f应用于x然后将结果函数应用于y,它有效地调用带有两个参数的内部函数)作为元组的替代。这种模拟多参数函数的方法称为currying。
#1
The ->
operator is for function types. a -> b
means "a
in, b
out", so float -> float
is the type of functions that take a float as their argument and produce a float as their result.
- >运算符用于函数类型。 a - > b表示“a in,b out”,所以float - > float是一个函数类型,它将float作为参数并生成一个float作为结果。
What about
float -> float -> float
那浮点数 - >浮点数 - >浮点数
->
is right-associative, so a -> b -> c
is the same as a -> (b -> c)
meaning a function that takes an a
and produces another function of type b -> c
. Functions like this are often used to simulate multi-arguments functions (you can use f x y
to apply f
to x
and then apply the resulting function to y
, which effectively calls the inner function with two arguments) as an alternative to tuples. This way of simulating multi-argument functions is called currying.
- >是右关联的,所以a - > b - > c与 - >(b - > c)相同,这意味着一个函数接受a并产生另一个类型为b - > c的函数。像这样的函数通常用于模拟多参数函数(你可以使用f x y将f应用于x然后将结果函数应用于y,它有效地调用带有两个参数的内部函数)作为元组的替代。这种模拟多参数函数的方法称为currying。