其结果仅取决于其参数的函数的名称是什么?

发布于 2024-08-18 05:10:57 字数 216 浏览 3 评论 0原文

我正在编写一个玩具编译器,如果结果仅取决于参数的值,它可以优化函数调用。因此,像 xor 和 concatenate 这样的函数仅依赖于它们的输入,使用相同的输入调用它们总是会给出相同的输出。但是像 time 和 rand 这样的函数依赖于“隐藏”的程序状态,并且使用相同的输入调用它们可能会给出不同的输出。我只是想弄清楚区分这两种类型的函数的形容词是什么,比如“同构”或“可重入”之类的。有人可以告诉我我要找的词吗?

I'm writing a toy compiler thingy which can optimise function calls if the result depends only on the values of the arguments. So functions like xor and concatenate depend only on their inputs, calling them with the same input always gives the same output. But functions like time and rand depend on "hidden" program state, and calling them with the same input may give different output. I'm just trying to figure out what the adjective is that distinguishes these two types of function, like "isomorphic" or "re-entrant" or something. Can someone tell me the word I'm looking for?

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

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

发布评论

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

评论(3

夜声 2024-08-25 05:10:57

The term you are looking for is Pure

木緿 2024-08-25 05:10:57

我认为它被称为纯函数

在计算机编程中,如果有关函数的以下两个陈述都成立,则可以将函数描述为纯函数:

  • 给定相同的参数值,该函数始终计算相同的结果值。函数结果值不能依赖于任何隐藏信息或状态,这些信息或状态可能会随着程序执行的进行或程序的不同执行之间发生变化,也不能依赖于来自 I/O 设备的任何外部输入。
  • 结果的评估不会导致任何语义上可观察到的副作用或输出,例如可变对象的突变或输出到 I/O 设备。

结果值不需要依赖于所有(或任何)参数值。但是,它必须只依赖于参数值。

I think it's called Pure Function:

In computer programming, a function may be described as pure if both these statements about the function hold:

  • The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change as program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices.
  • Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices.

The result value need not depend on all (or any) of the argument values. However, it must depend on nothing other than the argument values.

千里故人稀 2024-08-25 05:10:57

我想如果你用“纯函数”的话,你可以说这个形容词是“纯”。

我总是了解到,当参数始终相同时输出始终相同的函数称为“确定性”。就我个人而言,我认为这是一个更具描述性的术语。我猜想“纯函数”根据定义是确定性的,并且似乎纯函数也需要没有任何副作用。我认为并非所有确定性函数都是如此(只要相同参数的返回值始终相同)。

维基百科链接:http://en.wikipedia.org/wiki/Deterministic_algorithm

引用:

给定特定的输入,它将始终产生相同的输出,并且底层机器将始终经历相同的状态序列。

I guess you could say the adjective is "pure" if you go by "pure function".

I always learnt that a function whose output is always the same when the arguments are always the same is called "deterministic". Personally, I feel that that is a more descriptive term. I guess a "pure function" is by definition deterministic, and it seems a pure function is also required to not have any side-effects. I assume that that need not be the case for all deterministic functions (as long as the return value is always the same for the same arguments).

Wikipedia link: http://en.wikipedia.org/wiki/Deterministic_algorithm

Quote:

Given a particular input, it will always produce the same output, and the underlying machine will always pass through the same sequence of states.

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