功能编程中功能的执行顺序 [英] Order of execution of functions in functional programming

查看:45
本文介绍了功能编程中功能的执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从过去的6个月开始,我开始学习函数式编程的概念.在阅读许多资源时,我发现的一件事是在函数式编程中,执行顺序是不确定的!我不清楚这一点.

Since past 6 months, I started to learn the concepts of functional programming. While reading many sources, one of the things I found was that in functional programming, order of execution is undefined! I don't clearly understand this.

从另一个堆栈溢出答案中: https://stackoverflow.com/a/23290/1276021 ,"A函数语言"(理想情况下)允许您编写数学函数".

From this other Stack Overflow answer: https://stackoverflow.com/a/23290/1276021, "A functional language" (ideally) allows you to write a "mathematical function".

但是,就像在数学函数中一样, f(g(x))!== g(f(x))-含义,执行顺序很重要.

But, as in mathematical functions, f(g(x)) !== g(f(x)) - meaning, order of execution matter.

在理解执行顺序不确定"这一概念时,我似乎是错的.我在哪里错了?

It seems like I was wrong in understanding the concept that "order of execution is undefined". Where am I wrong?

推荐答案

f(g(x))!== g(f(x))-含义,执行顺序.

不,它们是不同的表达式/程序,不仅是不同的执行顺序.

No, those are different expressions/programs, not only different order of execution.

在理解执行顺序不确定"这一概念时,我似乎是错的.

It seems like I was wrong in understanding the concept that "order of execution is undefined"

正确,更冗长的陈述是" 评估表达式的各个部分无关紧要"(并且总是会导致相同的结果).这意味着这种功能语言没有副作用

The correct, more verbose statement would be "the order of evaluation of the parts of an expression is irrelevant" (and will always lead to the same result). This means that such a functional language has no side effects and referential transparency - two very important properties for proofs that "mathematical" functions have as well.

如果我们定义两个函数

f(x) = x*3
g(y) = y+1

然后尝试对表达式 f(g(1))求值,我们是否先做就没关系

and then try to evaluate the expression f(g(1)), it doesn't matter whether we first do

  f(g(1))
= f(1+1)
= f(2)
= 2*3
= 6

  f(g(1))
= g(1)*3
= (1+1)*3
= 2*3
= 6

无论我们首先应用哪种功能,我们始终会得出6.

We always arrive at 6, regardless of which of the functions we apply first.

这篇关于功能编程中功能的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆