是否可以将"if-then-else"(总是)替换为函数调用? [英] Could `if-then-else` (always) be replaced by a function call?

查看:56
本文介绍了是否可以将"if-then-else"(总是)替换为函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是出于对PL如何工作的好奇,而不是其他任何事情.(实际上,我在查看SML时遇到了我,它与Haskell的不同之处在于前者使用按值调用-但我的问题是关于Haskell的.)

This question is out of curiousity about how PLs work, more than anything else. (It actually came to me while looking at SML which differs from Haskell in that the former uses call-by-value - but my question is about Haskell.)

Haskell(据我了解)具有按需呼叫"的语义.这是否意味着如果我按以下方式定义函数:

Haskell (as I understand) has "call-by-need" semantics. Does this imply that if I define a function as follows:

cond True thenExp elseExp = thenExp 
cond _ thenExp elseExp = elseExp

这将始终与if-then-else表达式完全一样吗?或者,换一种说法,if-then-else是否可以被看作是可以被定义为函数的事物的语法糖?

that this will always behave exactly like an if-then-else expression? Or, in another sense, can if-then-else be regarded as syntactic sugar for something that could've been defined as a function?

仅是将Haskell与标准ML的行为进行对比,请定义(在SML中)

Just to contrast the behaviour of Haskell with Standard ML, define (in SML)

cond p t f =如果p则为else f;

然后是阶乘函数

有趣的事实n = cond(n = 0)1(n *事实(n-1));

评估事实1(例如)永远不会结束,因为 cond 的最后一个参数的递归永远不会终止.

evaluating factc 1 (say) never finishes, because the recursion in the last argument of cond never terminates.

但是,定义

有趣的事实n =如果n = 0则为1个其他事实(n-1);

按预期工作,因为仅根据需要评估了then分支.

works as we expect because the then branch is only evaluated as needed.

也许有聪明的方法可以推迟SML中的参数求值(不知道,因为我还不熟悉),但重点是在按值调用类型语言中,if-then-else经常表现不同.我的问题是,这(按需致电与按值致电)是否是造成这种差异的主要原因(而且共识似乎是是").

Maybe there are clever ways to defer argument evaluation in SML (don't know as I'm not that familiar with it yet) but the point is that in a call-by-value type language, if-then-else often behaves differently. My question was whether this (call-by-need vs call-by-value) was the principle reason behind this difference (and the consensus seems to be "yes").

推荐答案

Haskell一样 if-then-else 上的Wikipedia说:

Like the Haskell Wikipedia on if-then-else says:

对于处理条件,在Haskell98中定义了if-then-else语法.但是,可以简单地将其替换为功能"if"和
For processing conditions, the `if-then-else` **syntax was defined in Haskell98**. However it could be simply replaced by the function `if'` with
if' :: Bool -> a -> a -> a
if' True  x _ = x
if' False _ y = y

因此,如果我们使用上面的 if'函数,并且我们需要对其进行评估(由于Haskell是惰性的,所以我们不需要评估if - then - else 表达式),Haskell将首先评估第一个操作数,以确定它是 True 还是 False .如果它是 True ,它将返回第一个表达式,如果它是 False ,则将返回第二个表达式.请注意,这本身不是不是,这意味着我们(完全)评估这些表达式.仅当需要结果时,我们才对表达式进行求值.

So if we use the above if' function, and we need to evaluate it (since Haskell is lazy, we do not necessary need to evaluate an if-then-else expression at all), Haskell will first evaluate the first operand to decide if it is True or False. In case it is True, it will return the first expression, if it is False it will return the second expression. Note that this does not per se means that we (fully) evaluate these expressions. Only when we need the result, we will evaluate the expressions.

但是如果条件是 True ,则完全没有理由评估第二个表达式,因为我们忽略了它.

But in case the condition is True, there is no reason at all to ever evaluate the second expression, since we ignore it.

如果我们在表达式树的多个部分上共享一个表达式,则当然有可能另一个调用将(部分)对另一个表达式求值.

In case we share an expression over multiple parts of the expression tree, it is of course possible that another call will (partially) evaluate the other expression.

ghci 甚至可以选择覆盖 if< expr>然后< expr>其他< expr> 语法: -XRebindable 标志.除了其他方面,它还将:

ghci even has an option to override the if <expr> then <expr> else <expr> syntax: the -XRebindable flag. It will, besides other things also:

条件(例如 if e1则为e2 else e3 )表示 ifThenElse e1 e2 e3 .但是,大小写表达式不受影响.

Conditionals (e.g. if e1 then e2 else e3) means ifThenElse e1 e2 e3. However case expressions are unaffected.

这篇关于是否可以将"if-then-else"(总是)替换为函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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