了解Haskell函数签名 [英] Understanding Haskell function signature

查看:96
本文介绍了了解Haskell函数签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在简单的世界里它意味着什么:


在haskell的世界中,我遇到了一些新问题。

  add :: Integer  - >整数 - >整数

这是否意味着两个第一个参数是 Integer 和返回的值是整数呢?



你能解释我,使用箭头的目的确定一个参数的类型或者至少给我一个关于这个函数签名的简单解释?

解决方案

通常我们可能会参考 add 作为一个函数,它接受两个整数 s并产生一个整数。但是为了理解符号,你需要理解在技术上不存在这样一个函数,它在Haskell中有两个参数。



每个函数只需要一个参数,而类型的函数被写入 a - > r 其中 a 是参数的类型, r 是结果的类型。函数箭头是右关联的,这意味着类型 a - > (b - > c)可以不带圆括号书写为 a - > b - > c



因此整数 - >整数 - >整数整数 - >相同。 (Integer - > Integer),它告诉我们 add 是一个函数,它接受整数并产生另一个类型为 Integer - >的函数。整数。这就是所谓的currying,并且是在Haskell中编码多参数函数的常用方法。



要调用这样一个curry函数,我们可以写添加1 2 ,这是因为函数应用程序是关联的,与(add 1)2 相同,首先调用 add 1 得到一个类型为 Integer - >的函数。整数,然后将该函数应用于参数 2


I m new in haskell's world and I m having some troubles with function signatures :

What does it mean in simple worlds :

add:: Integer -> Integer -> Integer

Does it mean that the two first parameters are Integer and the returned value is Integer too ?

Can you explain me, the aim of using arrows to determine a type of parameter or at least give me a brief explanation about this function signature ?

解决方案

Colloquially we may indeed refer to add as a function that takes two Integers and produces an Integer. However to understand the notation, you need to understand that technically there's no such thing as a function that takes two arguments in Haskell.

Rather every function takes exactly one argument and the type of a function is written a -> r where a is the type of the argument and r is the type of the result. The function arrow is right-associative, which means that the type a -> (b -> c) can be written without parentheses as a -> b -> c.

So Integer -> Integer -> Integer is the same as Integer -> (Integer -> Integer), which tells us that add is a function that takes an Integer and produces another function of type Integer -> Integer. This is known as currying and is the usual way to "encode" multi-argument functions in Haskell.

To call such a curried function, we can write add 1 2, which, because function application is left associative, is the same as (add 1) 2, first calling add 1 to get a function of type Integer -> Integer and then applying that function to the argument 2.

这篇关于了解Haskell函数签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆