' 的含义 在 Haskell 函数名中?

发布于 2024-07-29 21:39:22 字数 228 浏览 5 评论 0原文

引用 ' 的用途是什么? 我已经阅读了有关柯里化函数的内容,并阅读了定义 add 函数的两种方法 - 柯里化和非柯里化。 柯里化版本...

myadd' :: Int -> Int -> Int
myadd' x y = x + y

...但在没有引用的情况下它也同样有效。 那么'有什么意义呢?

What is quote ' used for? I have read about curried functions and read two ways of defining the add function - curried and uncurried. The curried version...

myadd' :: Int -> Int -> Int
myadd' x y = x + y

...but it works equally well without the quote. So what is the point of the '?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

雄赳赳气昂昂 2024-08-05 21:39:22

这句话对哈斯克尔来说毫无意义。 它只是该函数名称的一部分。

人们倾向于将其用于“内部”功能。 如果您有一个使用累加器参数对列表求和的函数,则您的 sum 函数将采用两个参数。 这很丑陋,所以你创建了一个包含两个参数的 sum' 函数,以及一个包含一个参数的 sum 函数,例如 sum list = sum' 0 list

编辑,也许我应该只显示代码:

sum' s [] = s
sum' s (x:xs) = sum' (s + x) xs

sum xs = sum' 0 xs

您这样做是为了使 sum' 是尾递归的,并且“公共 API”看起来很漂亮。

The quote means nothing to Haskell. It is just part of the name of that function.

People tend to use this for "internal" functions. If you have a function that sums a list by using an accumulator argument, your sum function will take two args. This is ugly, so you make a sum' function of two args, and a sum function of one arg like sum list = sum' 0 list.

Edit, perhaps I should just show the code:

sum' s [] = s
sum' s (x:xs) = sum' (s + x) xs

sum xs = sum' 0 xs

You do this so that sum' is tail-recursive, and so that the "public API" is nice looking.

李不 2024-08-05 21:39:22

它通常发音为“prime”,因此将是“myadd prime”。 它通常用于表示计算的下一步或替代方案。

所以,你可以说

add = blah
add' = different blah

或者

f x = 
  let x' = subcomputation x
  in blah.

它只是一种习惯,就像在 Java、C 等的 for 循环中使用 int i 作为索引。

编辑:既然我已经添加了所有单词和代码格式,这个答案希望更有帮助。 :) 我一直忘记这不是一个所见即所得的系统!

It is often pronounced "prime", so that would be "myadd prime". It is usually used to notate a next step in the computation, or an alternative.

So, you can say

add = blah
add' = different blah

Or

f x = 
  let x' = subcomputation x
  in blah.

It just a habit, like using int i as the index in a for loop for Java, C, etc.

Edit: This answer is hopefully more helpful now that I've added all the words, and code formatting. :) I keep on forgetting that this is not a WYSIWYG system!

孤独患者 2024-08-05 21:39:22

在本例中,' 字符没有特殊意义; 它只是标识符的一部分。 换句话说,myaddmyadd' 是不同的、不相关的函数。

但按照惯例,' 用于表示某种逻辑求值关系。 因此,假设函数 myaddmyadd' 是相关的,这样 myadd' 可以从 myadd 派生。 这是源自学术界(Haskell 的根源)的形式逻辑和证明的惯例。 我应该强调,这只是一个约定,Haskell 并不强制执行它。

There's no particular point to the ' character in this instance; it's just part of the identifier. In other words, myadd and myadd' are distinct, unrelated functions.

Conventionally though, the ' is used to denote some logical evaluation relationship. So, hypothetical function myadd and myadd' would be related such that myadd' could be derived from myadd. This is a convention derived from formal logic and proofs in academia (where Haskell has its roots). I should underscore that this is only a convention, Haskell does not enforce it.

可爱暴击 2024-08-05 21:39:22

quote ' 只是 Haskell 名称中另一个允许的字符。 它通常用于定义函数的变体,在这种情况下,quote 发音为“prime”。 具体来说,Haskell 库使用 quote-variants 来表明变体是严格的。 例如:foldl 是惰性的,foldl' 是严格的。

在这种情况下,看起来引号只是用于分隔柯里化和非柯里化变体。

quote ' is just another allowed character in Haskell names. It's often used to define variants of functions, in which case quote is pronounced 'prime'. Specifically, the Haskell libraries use quote-variants to show that the variant is strict. For example: foldl is lazy, foldl' is strict.

In this case, it looks like the quote is just used to separate the curried and uncurried variants.

孤城病女 2024-08-05 21:39:22

正如其他人所说, ' 对 Haskell 本身没有任何意义。 它只是一个字符,就像字母或数字一样。

' 用于表示函数的替代版本(在 foldlfoldl' 的情况下)或辅助函数。 有时,您甚至会在函数名称上看到几个 '。 在函数名称末尾添加 ' 比编写 someFunctionHelpersomeFunctionStrict 更加简洁。

这种表示法起源于数学和物理学,如果有一个函数 f(x),它的导数通常表示为 f'(x)

As said by others, the ' does not hold any meaning for Haskell itself. It is just a character, like the a letter or a number.

The ' is used to denote alternative versions of a function (in the case of foldl and foldl') or helper functions. Sometimes, you'll even see several ' on a function name. Adding a ' to the end of a function name is just much more concise than writing someFunctionHelper and someFunctionStrict.

The origin of this notation is in mathematics and physics, where, if you have a function f(x), its derivate is often denoted as f'(x).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文