gpt4 book ai didi

functional-programming - 副作用是好事吗?

转载 作者:行者123 更新时间:2023-12-03 09:32:00 25 4
gpt4 key购买 nike

关闭。这个问题是opinion-based .它目前不接受答案。












想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题.

去年关闭。




Improve this question




我觉得这个词相当贬义。因此,我对维基百科中的两句话感到震惊:

Imperative programming is known for employing side effects to make programs function. Functional programming in turn is known for its minimization of side effects. [1]



由于我有点偏向数学,后者听起来很棒。副作用的论据是什么?它们是指失去控制还是接受不确定性?它们是好东西吗?

最佳答案

每隔一段时间我就会看到一个关于 SO 的问题,这迫使我花半个小时来编辑一篇非常糟糕的维基百科文章。这篇文章现在只是中等程度的糟糕。在与你的问题有关的部分中,我写道:

In computer science, a function or expression is said to have a side effect if, in addition to producing a value, it also modifies some state or has an observable interaction with calling functions or the outside world. For example, a function might modify a global or a static variable, modify one of its arguments, raise an exception, write data to a display or file, read data, call other side-effecting functions, or launch missiles. In the presence of side effects, a program's behavior depends on past history; that is, the order of evaluation matters. Because understanding an effectful program requires thinking about all possible histories, side effects often make a program harder to understand.

Side effects are essential to enable a program to interact with the outside world (people, filesystems, other computers on networks). But the degree to which side effects are used depends on the programming paradigm. Imperative programming is known for uncontrolled, promiscuous use of side effects. In functional programming, side effects are rarely used. Functional languages such as Standard ML and Scheme do not restrict side effects, but it is customary for programmers to avoid them. The functional language Haskell restricts side effects with a static type system; only a function that produces a result of IO type can have side effects.

关于functional-programming - 副作用是好事吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/763835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com