为什么sum x y是type(Num a)=> a - > a - >一个在Haskell中? [英] Why sum x y is of type (Num a) => a -> a -> a in Haskell?

查看:144
本文介绍了为什么sum x y是type(Num a)=> a - > a - >一个在Haskell中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读关于Haskell的知识,并且我很难理解函数定义是如何在这种语言中处理的。



假设我正在定义 sum 函数:

  let sum xy = x + y 

如果我查询Haskell的类型

 :t sum 

我得到

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




  1. 这是什么意思 => ; 运算符?它与lambda表达式有什么关系?这就是在C#中表示 => 运算符后面的内容是一个的原因。

  2. a - >> a - >一个是什么意思?通过对一些不同的功能进行眼睛检查,我已经尝试过了,它似乎是最初的 a - > a 是参数,最后 - >一个是和函数的结果。如果这是正确的,为什么不是(a,a) - >一个,这看起来更直观?


解决方案

0。 Haskell => 与C#的 => 无关。在Haskell中,一个匿名函数被创建为

  \ x  - > x * x 

另外,不要命名函数 sum ,因为此类一个函数已经存在于Prelude中。为了避免混淆,我们现在称之为



1。 Haskell中的 => 为该类型提供了一个上下文。例如:

  show ::(显示a)=> a  - >字串

这里,显示a => 表示 a 类型必须是类型类的实例 显示,这意味着 a 必须可转换为字符串。类似地,(Num a)=> a - > a - >一个表示 a 类型必须是类型类型Num的一个实例,这意味着 a 必须像一个数字。这对 a 施加了一个约束,使得 show 加上不会接受一些不受支持的输入,例如加上56abc。 (字符串不像数字。)



类型类与C#的接口类似,或者更具体地说,是一个泛型中的接口基类型约束。有关详细信息,请参阅 解释Haskell中的类型类 的问题。 。

<2> a - > a - > a 表示 a - > (a - > a)。因此,它实际上是一个返回另一个函数的一元函数。

  plus x = \y  - > x + y 

这使得部分应用(currying)非常容易。部分应用程序被大量使用。当使用高阶函数时。例如,我们可以使用

  map(加4)[1,2,3,4] 

将4添加到列表的每个元素。事实上,我们可以再次使用部分应用程序来定义:

  plusFourToList :: Num a => [a]  - > [a] 
plusFourToList = map(加4)

如果函数被写入形式(a,b,c,...) - > z 默认情况下,我们必须引入很多lambda表达式:

  plusFourToList = \l  - > map(\y-> plus(4,y),1)


I've been reading about Haskell and I'm having a hard time understanding how function definitions are handled in this language.

Let's say I'm defining a sum function:

let sum x y = x + y

if I query Haskell for its type

:t sum

I get

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

  1. What does it mean the => operator? Does it have anything to do with lambda expressions? That's how one signals that what is following the => operator is one, in C#.
  2. What does the a -> a -> a mean? By eye inspection on a number of different functions I've been trying out, it seems the initial a -> a are the arguments and the final -> a is the result of the sum function. If that is right, why not something as (a, a) -> a, which seems way more intuitive?

解决方案

0. The Haskell => has nothing to do with C#'s =>. In Haskell an anonymous function is created with

\x -> x * x

Also, don't name the function sum because such a function already exists in Prelude. Let's call it plus from now on to avoid confusion.

1. Anyway, the => in Haskell provides a context to the type. For instance:

show :: (Show a) => a -> String

Here, The Show a => means a type must be an instance of the type class Show, which means a must be convertible to a string. Similarly, (Num a) => a -> a -> a means the a type must be an instance of the type class Num, which means a must be like a number. This puts a constraint on a so that show or plus won't accept some unsupported input, e.g. plus "56" "abc". (String is not like a number.)

A type class is similar to C#'s interface, or more specifically, an interface base-type constraint in generics. See the question Explain Type Classes in Haskell for more info.

2. a -> a -> a means a -> (a -> a). Therefore, it is actually a unary function that returns another function.

plus x = \y -> x + y

This makes partial application (currying) very easy. Partial application is used a lot esp. when using higher order functions. For instance we could use

map (plus 4) [1,2,3,4]

to add 4 to every element of the list. In fact we could again use partial application to define:

plusFourToList :: Num a => [a] -> [a]
plusFourToList = map (plus 4)

If a function is written in the form (a,b,c,...)->z by default, we would have to introduce a lot of lambdas:

plusFourToList = \l -> map(\y -> plus(4,y), l) 

这篇关于为什么sum x y是type(Num a)=&gt; a - &gt; a - &gt;一个在Haskell中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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