< = vs.=>的含义在Haskell [英] The meaning of <= vs. => in Haskell

查看:48
本文介绍了< = vs.=>的含义在Haskell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Haskell比较陌生;< = 语法代表什么,< = => 之间有什么区别?两者的例子都会有所帮助.

I'm relatively new to Haskell; what does the <= syntax represent and what is the difference between <= and =>? Examples of both would be helpful.

推荐答案

两者是完全无关的;它们似乎只是因为ASCII而相关.如果您看一下它们的Unicode等效项,那就更有意义了:

The two are completely unrelated; they just seem related because of ASCII. It makes more sense if you look at their Unicode equivalents:

  • => 是箭头:.它用于指定类型签名中的约束:

  • => is an arrow: . It's used to specify constraints in type signatures:

Eq a => a -> a -> Bool

上述签名中的 Eq a => 意味着类型变量 a 可以是作为 Eq 类.也就是说,任何具有 deriving(Eq)或显式实例(例如 instance Eq Type where ... .

The Eq a => in the above signature means that the type variable a can be any type that is an instance of the Eq class. That is, any type that either has deriving (Eq) or an explicit instance like instance Eq Type where ....

在函数签名中,-> 指定普通参数,而 => 指定签名中的约束.在上面的示例( Eq a => a-> gt-a-> Bool )中,该函数接受两个 a 类型的参数,并为我们提供了布尔. Eq a => 部分不是该函数的显式参数;它是函数的显式参数.它只是告诉我们 a 必须是 Eq 的一部分(也就是说,它必须与 == 相当).

In function signatures, -> specifies a normal argument while => specifies constraints in the signature. In the above example (Eq a => a -> a -> Bool), the function takes two arguments of type a and gives us a Bool. The Eq a => part is not an explicit argument to the function; it just tells us that a must be part of Eq (that is, it must be comparable with ==).

< = 小于或等于.也就是说,它是,不是 .这是标准库中属于 Ord 类的一部分的普通函数:

<= is less than or equal to. That is, it's , not . It's a normal function in the standard library that's part of the Ord class:

λ> :t (<=)
(<=) :: Ord a => a -> a -> Bool

您可以在正则表达式中使用它:

You can use it in a normal expression:

λ> 10 <= 12
True

它们看上去对称的唯一原因是因为的ASCII近似值是相同的,但这只是符号上的限制.否则,它们是完全不相关的.

The only reason they seem symmetrical is because the ASCII approximation of and are the same, but that's just a limitation in notation. Otherwise, they're completely unrelated.

您可以在代码中使用明确的Unicode符号. UnicodeSyntax 扩展名允许对 => 使用,并且 base-unicode-symbols 软件包包含Unicode版本的标准库函数,包括用于< = .

You can use the unambiguous Unicode symbols in your code. The UnicodeSyntax extension enables using for => and the base-unicode-symbols package contains Unicode versions of standard library functions including for <=.

这篇关于&lt; = vs.=&gt;的含义在Haskell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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