简体   繁体   English

Haskell ' 符号有什么作用?

[英]Haskell what does the ' symbol do?

As the title states, I see pieces of code online where the variables/functions have ' next to it, what does this do/mean?正如标题所述,我在网上看到了变量/函数旁边有 ' 的代码片段,这是做什么/意味着什么? ex:前任:

function :: [a] -> [a]
function ...

function' :: ....

As the other answers said, function' is just another variable name.正如其他答案所说, function'只是另一个变量名。 So,所以,

 don'tUse :: Int -> IO ()
 don'tUse won'tBe''used'' = return ()

is just like就像

 dontUse :: Int -> IO ()
 dontUse wontBeUsed = return ()

with slightly different names.名称略有不同。 The only requirement is that the name starts with a lowercase-letter or underscore, after that you can have as many single-quote characters as you want.唯一的要求是名称以小写字母或下划线开头,之后您可以使用任意数量的单引号字符。

Prelude> let _' = 1
Prelude> let _'' = 2
Prelude> let _''''''''' = 9
Prelude> _' + _'' * _'''''''''
19

...Of course it's not necessarily a good idea to name variables like that; ...当然,这样命名变量不一定是个好主意; normally such prime-names are used when making a slightly different version of an already named thing .通常,在制作已命名事物的稍微不同版本时会使用此类素名。 For example, foldl and foldl' are functions with the same signature that do essentially the same thing, only with different strictness (which often affects performance memory usage and whether infinite inputs are allowed, but not the actual results).例如, foldlfoldl'是具有相同签名的函数,它们基本上做相同的事情,只是严格性不同(这通常会影响性能 memory 的使用以及是否允许无限输入,但不会影响实际结果)。

That said, to the question也就是说,对于这个问题

Haskell what does the ' symbol do? Haskell '符号有什么作用?

– the ' symbol does in fact do various other things as well, but only when it appears not as a non-leading character in a name. '符号实际上也可以做各种其他事情,但只有当它不作为名称中的非前导字符出现时。

  • 'a' is a character literal . 'a'字符文字
  • 'Foo is a constructor used on the type level . 'Foo在类型级别上使用的构造函数 See DataKinds .请参阅数据种类
  • 'bar and ''Baz are quoted names . 'bar''Baz引用的名称 See TemplateHaskell .请参阅TemplateHaskell

The notation comes from mathematics.符号来自数学。 It is read x prime .读作x prime In pretty much any math manual you can find something like let x be a number and x' be the projection of... (math stuff) .在几乎任何数学手册中,您都可以找到类似let x be a number and x' be the projection of... (math stuff)

Why not using another convention?为什么不使用其他约定? well, in mathematics It makes a lot of sense because It can be very pedagogical... In programming we aren't used to this convention so I don't see the point of using it, but I am not against it neither.好吧,在数学中它很有意义,因为它可以非常具有教学性......在编程中,我们不习惯这种约定,所以我看不出使用它的意义,但我也不反对它。

Just to give you an example of its use in mathematics so you can understand why It is used in Haskell.只是给你一个它在数学中使用的例子,这样你就可以理解为什么它在 Haskell 中使用。 Below, the same triangle concept but one using prime convention and other not using it.下面是相同的三角形概念,但一个使用素数约定,另一个不使用它。 It is pretty clear in the first picture that pairs ( A , A' ), ( B , B' ), ... are related by one being the vertex and the prime version being the midpoint of the oposite edge.在第一张图片中很清楚,对 ( A , A' ), ( B , B' ), ... 通过一个是顶点和主要版本是对边的中点相关联。 Whereas in the second example, you just have to remember that A is the midpoint of the oposite edge of vertex P .而在第二个示例中,您只需要记住A是顶点P的对边的中点。 First is easier and more pedagogical:首先是更容易和更具教学性:

在此处输入图像描述

在此处输入图像描述

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

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