The meaning of ' in Haskell function name?

女生的网名这么多〃 提交于 2019-12-18 20:07:25

问题


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 '?


回答1:


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.




回答2:


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!




回答3:


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.




回答4:


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.




回答5:


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).



来源:https://stackoverflow.com/questions/1207649/the-meaning-of-in-haskell-function-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!