什么= =>符号在Haskell中的意思? [英] What does the => symbol mean in Haskell?

查看:242
本文介绍了什么= =>符号在Haskell中的意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Haskell的新手,通常是函数式编程,我对它的语法有点不舒服。

在下面的代码中, => 表示?还有(Num a,Ord a)?

  loop :: (Num a,Ord a)=> a  - > (t  - > t) - > t  - > t 


解决方案

这是一个typeclass约束; (Num a,Ord a)=> ... 表示 loop 可以与任何类型 a 一起使用,它是 Num Ord 类型类型,分别对应于数字类型和有序类型。基本上,您可以将 loop 看作 =>> 右边的类型,除了 a 必须是 Num Ord 的实例。 。

你可以认为类型类与OOP接口基本相似(但它们不是一回事!) - 它们封装了一组必须支持的定义,并且可以使用这些定义来编写通用代码。例如, Num 包括数字操作,如加法和乘法,而 Ord 包含小于,大于等等。



有关类型类的更多信息,请参见 了解你是一个Haskell 的介绍。


I'm new to Haskell and, in general, to functional programming, and I'm a bit uncomfortable with its syntax.

In the following code what does the => denote? And also (Num a, Ord a)?

loop :: (Num a, Ord a) => a -> (t -> t) -> t -> t

解决方案

This is a typeclass constraint; (Num a, Ord a) => ... means that loop works with any type a that is an instance of the Num and Ord typeclasses, corresponding to numeric types and ordered types respectively. Basically, you can think of loop as having the type on the right hand side of the =>, except that a is required to be an instance of Num and Ord.

You can think of typeclasses as basically similar to OOP interfaces (but they're not the same thing!) — they encapsulate a set of definitions which any instance must support, and generic code can be written using these definitions. For instance, Num includes numeric operations like addition and multiplication, while Ord includes less than, greater than, and so on.

For more information on typeclasses, see this introduction from Learn You a Haskell.

这篇关于什么= =>符号在Haskell中的意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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