哪些函数式编程术语可以区分避免修改变量和对象?

发布于 2024-10-28 02:16:40 字数 449 浏览 2 评论 0原文

在函数式编程中,使用什么术语来区分避免修改变量引用的内容和避免修改对象本身?

对于示例, 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 技术交流群。

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

发布评论

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

评论(2

心碎的声音 2024-11-04 02:16:40

使用名称来引用其在封闭/先前范围内所做的以外的其他内容称为“隐藏”该名称。它确实与突变不同。例如,在 Haskell 中,我可以编写

return 1 >>= \x -> return (x + 1) >>= \x -> print x.

打印的 x 是由第二个 lambda 引入的,即 2

在 do 表示法中,这看起来有点熟悉:

foo = do
  x <- return 1
  x <- return (x + 1)
  print x

据我了解,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

return 1 >>= \x -> return (x + 1) >>= \x -> print x.

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:

foo = do
  x <- return 1
  x <- return (x + 1)
  print x

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

遥远的绿洲 2024-11-04 02:16:40

我认为函数式编程语言根本没有任何破坏性更新源操作数之一的运算符(也许是破坏性更新,您正在寻找的术语?)。在指令集设计中也可以看到类似的理念: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.

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