哪些函数式编程术语可以区分避免修改变量和对象?
在函数式编程中,使用什么术语来区分避免修改变量引用的内容和避免修改对象本身?
对于示例, Ruby
name += title
避免修改先前由 name
引用的对象,而是创建一个新对象,但遗憾的是让 name
引用新对象,而
full_title = name + title
不仅避免修改对象,而且避免修改 name
所指的内容。
对于避免前者的代码,您会使用什么术语?
In functional programming, what terminology is used to distinguish between avoiding modifying what a variable refers to, and avoiding modifying an object itself?
For example, in Ruby,
name += title
avoids modifying the object previously referred to by name
, instead creating a new object, but regrettably makes name
refer to the new object, whereas
full_title = name + title
not only avoids modifying objects, it avoids modifying what name
refers to.
What terminology would you use for code that avoids the former?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用名称来引用其在封闭/先前范围内所做的以外的其他内容称为“隐藏”该名称。它确实与突变不同。例如,在 Haskell 中,我可以编写
打印的
x
是由第二个 lambda 引入的,即2
。在 do 表示法中,这看起来有点熟悉:
据我了解,Erlang 完全禁止别名。
然而,我怀疑数学对于 Ruby 来说是正确的——它不仅掩盖了名字,还改变了一些底层的对象。另一方面,我不太了解 Ruby...
Using a name to refer to something other than what it did in an enclosing/previous scope is known as "shadowing" that name. It is indeed distinct from mutation. In Haskell, for example I can write
The
x
that is printed is the one introduced by the second lambda, i.e.,2
.In do notation this looks a bit more familiar:
As I understand it, Erlang forbids aliasing altogether.
However, I suspect that mathepic is right in terms of Ruby -- its not just shadowing the name but mutating some underlying obect. On the other hand, I don't know Ruby that well...
我认为函数式编程语言根本没有任何破坏性更新源操作数之一的运算符(也许是破坏性更新,您正在寻找的术语?)。在指令集设计中也可以看到类似的理念:RISC 理念(越来越多地用于甚至 x86 架构(在较新的扩展中)是为二元运算符提供三操作数指令,如果您想要破坏性更新,则必须显式指定目标操作数与源操作数之一相同。
对于后者,一些混合语言(例如 Scala;在 X10) 区分值 (
val
) 和变量(var
)。前者不能重新分配,后者可以。如果它们指向一个可变对象,那么当然该对象本身仍然可以被修改。I think functional programming languages simply do not have any operators that destructively updates one of the source operands (is destructive update, perhaps, the term you're looking for?). A similar philosophy is seen in instruction set design: the RISC philosophy (increasingly used in even the x86 architecture, in the newer extensions) is to have three-operand instructions for binary operators, where you have to explicitly specify that the target operand is the same as one of the sources if you want destructive update.
For the latter, some hybrid languages (like Scala; the same terminologies are used in X10) distinguish between values (
val
) and variables (var
). The former cannot be reassigned, the latter can. If they point to a mutable object, then of course that object itself can still be modified.