简体   繁体   English

Haskell中“+”的类型是什么意思

[英]What does the type of “+” mean in Haskell

Prelude> :t (+)
(+) :: (Num a) => a -> a -> a

My lecture slide says that 我的演讲幻灯片说

a -> a -> a

means a function take two parameters and return one, and all of them are the same type. 表示函数接受两个参数并返回一个,并且它们都是相同的类型。 Which two are the parameters and which one is the return value? 哪两个是参数,哪一个是返回值?

Thank you. 谢谢。

There are some levels you have to master here: 你必须掌握一些关卡:

level 0 0级

a -> b -> c

is a function taking one a and one b and producing one c 是一个函数取一个a和一个b并产生一个c

level 1 1级

well there is more to it: 那还有更多:

a -> b -> c

which is really 这是真的

a -> (b -> c)

is a function taking one a and producing another function, that takes a b and produces a c 是一个函数取一个a并产生另一个函数,它取b并产生一个c

level 2 2级

f :: (Num a) => a -> a -> a

Adds a constraint to a (here Num - this means that a should be a number - a is an instance of the Num type-class) a添加约束(此处为Num - 这意味着a应该是数字 - aNum类型的实例)

So you get a function that takes an a and produces a function that takes another a and returns a a , and a needs to be an instance of Num 所以,你得到一个函数,它的a ,并产生一个函数,另需a ,并返回a ,和a需要是一个实例Num

so every input to f has to be of the same type of number: 所以f每个输入都必须是相同类型的数字:

  • f 1 2 is ok f 1 2
  • f 'a' 'b' is not ok f 'a' 'b' 不行
  • f (1::Int) (2::Int) is ok f (1::Int) (2::Int)
  • f (1::Float) (2::Float) is ok f (1::Float) (2::Float)
  • f (1::Int) (2::Float) is not ok f (1::Int) (2::Float) 不行

level 3 (understanding (+) ) 3级(理解(+)

The last thing you have to understand here is that, (+) is defined as a part of Num so there are different + based on the used types ... and the same is true for the number literals like 0 , 1 , ... thats why 0 can be a Float or a Int or whatever type that is a instance of Num 你必须了解这里的最后一件事是, (+)被定义为一部分Num所以有不同的 +基于所使用的类型......而同样适用于数字文字像真正的01 ,...那就是为什么0可以是FloatInt或者是Num实例的任何类型

The first two are parameters, the last one is the return value. 前两个是参数,最后一个是返回值。

In fact, due to currying, it can be read like this: the + function (which only accepts numeric values) takes a parameter a and returns a function that takes a parameter of the same type and returns the result of the same type. 事实上,由于currying,它可以这样读: +函数(只接受数值)接受参数a返回一个函数该函数接受相同类型的参数并返回相同类型的结果。

Here's a contrived example: 这是一个人为的例子:

let addTwo = (+) 2 -- the + function takes one argument and returns a function
addTwo 3 -- we can add the second argument here and obtain 5 as returned value

Suppose we have a type like this: 假设我们有这样的类型:

a -> b -> c -> d -> e

The last thing in the sequence is the return type. 序列中的最后一件事是返回类型。 So this function returns something of type e . 所以这个函数返回e类型的东西。 Everything else is the argument types. 其他一切都是参数类型。 So this function takes 4 arguments, who's types are a , b , c and d . 所以这个函数有4个参数,谁的类型是abcd

Lower-case letters denote "type variables" — variables which can stand for any type. 小写字母表示“类型变量” - 可以代表任何类型的变量。 (It doesn't have to be a single letter, but it often is.) Anything beginning with an upper-case letter is a specific type, not a variable. (它不必是单个字母,但通常是。)任何以大写字母开头的东西都是特定类型,而不是变量。 (For example, Int is a type, int is a type variable.) (例如, Int是一个类型, int是一个类型变量。)

The Num a part means that a stands for any type, but that type must implement the Num type-class. Num a part表示a代表任何类型,但该类型必须实现Num类型。 Other common contexts are Eq (defines the == operator), Ord (defines < , > , and so forth) and Show (defines the show function that converts stuff into a string). 其他常见的上下文是Eq (定义==运算符), Ord (定义<>等)和Show (定义将函数转换为字符串的show函数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM