如何命名 Haskell 表达式的“=”?

How to name the "=" of a Haskell expression?

let a = 3

是否表示:

如果Haskell中的所有内容都是函数,那么我可以在这里说a也是函数吗?但是函数是可以调用的,怎么调用"a"?

首先,为了解决这个问题,并非 Haskell 中的所有内容都是函数。函数实际上是它们自己的类型,由 (->) 运算符表示。这就是函数的类型声明看起来像 func :: a -> b 的方式。但是,Haskell 中的大多数内容都是惰性的,这意味着直到实际需要时才会评估值。这类似于无参数函数在其他语言中的作用方式,因此至少出于某些目的,值可以 概念化 作为函数。但是,这并不意味着您示例中的 a 是可调用的。

至于措辞,后两者中的任何一个都是准确的。我更喜欢第二种,但两者都有效。在 Haskell 中,let a = 3 并没有真正 构造 一个论点,所以第一个没有多大意义。 (不是大多数 Haskell 程序员工作的水平;我不知道生成的机器代码在幕后是如何工作的)。

总而言之,我建议您去学习一些关于 Haskell 的入门课程。他们中的大多数都涵盖了这类内容。

编辑:正如其他一些答案所指出的,"assign" 这个词往往暗示某种程度的可变性,而 Haskell 则没有。因此,我建议不要使用最后的措辞。

let a=3

"Construct an object named a"

不,这不是 Haskell 中的常用术语。

"bind value 3 to object a"

这更近了。可以简单地说 "bind value 3 to name (or variable, or identifier) a"。 Haskell.

中没有"objects"

"assign a value to variable a"

不,"assignment" 通常意味着可以改变变量的值,而在纯函数式编程中并非如此。

就我个人而言,我只是将 let a = 3 读作 "define a as 3"。在 Haskell 中,大多数时候,我们都在推理变量的值,而不管这些值是何时构造/分配/垃圾收集的,以及这些值在内存中的表示方式。所以,我们通常避免从操作上去考虑"how"一个程序是否执行,更多的关注"what"是结果,是否是预期的("denotationally")。

当然,在我们得到预期的结果之后,我们开始关注我们程序的性能。为此,我们至少需要对程序执行的"how"、内存分配等有一个大概的了解

据我所知,可变并不意味着事物是可变的,而是说价值不是固定的,取决于上下文。例如,当您定义数学函数 f(x) : x -> x+1 时,x 的可变方式取决于您在调用它时传递给 f 的内容,而 f 将总是相同的功能。没有可变性的概念,只有范围的概念。在 Haskell 中,我想事情是一样的,变量是任何 'binding' ,它不是全局的,但存在于有限的范围内。我会使用 defines 但我不明白为什么 'assign' 不合适。

Does it mean:

"Construct an object named a" or "bind value 3 to object a" or "assign a value to variable a"?

不,不,再一次,不。

首先,在 Haskell 中我们说的是 ,而不是对象。例如,数字是值,您知道每个数字都有无限多个名称。例如:

0
the predecessor of 1
the predecessor of the precdecessor of 2
the predecessor of ...(32493 times)... the predecessor of 32495
the product of 1 and 0
the product of the successor of 1 and 0
...
... and so on, you get the idea
...

现在,就像在正常语言中一样,当我们需要经常提及名称复杂的事物时,我们往往会发明一个简短的名称。例如,当我们写一篇关于 "bureau for the promotion of alcohol, tobacco and firearms" 活动的文章时,我们会引入一个缩写,如 in

The bureau for the promotion of alcohol, tobacco and firearms (ATF) is agian in the news. The chief of the ATF, Drink A'Glass, said in an interview that beer alone is no good when you can't have a good smoke at the same time. ...

同样,在Haskell中,我们可能需要提到"the square of the first element of the tuple that is the result of applying function f to some list whose name is xs",我们可以给它起这样的名字

let a = sqr . fst . f $ xs

以后我们可以只使用a。 由于引用透明性,如果您这样做并使用名称a 或一直编写复杂的表达式,对于程序的意义并不重要。因此,该原则保证名称的含义及其定义的含义相同,就像 "the predecessor of 2" 和 "the successor of 0" 与 1.

相同一样