“if”的方案中的等价性是什么? [英] What is the equivalence in scheme for 'if'?

查看:181
本文介绍了“if”的方案中的等价性是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码的方案中的等价性是什么?

What's the equivalence in scheme for the following code?

if (condition)
{
  function1;
  function2;
}
else
{
  function3;
  function4;
}

谢谢。

推荐答案

这取决于。你真的应该尝试用方案中的实际问题进一步缩小你的问题,因为答案将取决于你想要做什么。

It depends. You really should try to narrow you question further with an actual problem in scheme since the answer will depend on what you are trying to do.

在惯用方案中,大多数应该完成没有副作用所以你有

In idiomatic Scheme most should be done without side effects so you have

(if predicate-expression
    consequent-expression
    alternative-expression) ;; alternative is optional but should be used anyway.

但是对于你的代码,你在分支机构中有不止一件事,比如:

But with your code you have more than one thing in the branches, something like:

int test (condition)
{
    if (condition)
    {
        function1();
        return function2();
    }
    else
    {
        function3();
        return function4();
    }
}

此处,为了 function1 function3 做一些有用的事情,它有变异,读或写的东西,也称为副作用。你应该尽量避免这种情况,所以在Scheme中你要么使用来存储一个临时变量,所以当它返回时执行调用就像这样使用:

Here, in order for function1 and function3 to do anything useful it has to mutate, read or write something, also called a side effect. You should try to avoid that so in Scheme you either use a let to store a temporary variable, do the call so when it returns it is used like this:

(define (test condition)
  (if condition
      (let ((tmp (function3)))
        (function4 tmp))
      (function4 (function3))))

现在。你最终需要一些程序的副作用,然后你需要使用开始或使用 cond 这有明确的开始。

Now. You will eventually need side effects for a few procedures and then you need to use begin or use cond which has explicit begin.

(if predicate-expression
    (begin 
       consequent-side-effect-expression
       consequent-tail-expression)
    (begin
       alternative-side-effect-expression 
       alternative-tail-expression))

现在开始将几个表达式连接成一个,开始块的结果是它的最后一个表达式,因此你可以甚至在谓词中使用它。 Cond在每个结果中都有明确的开头因此我经常从切换到 cond 当我需要多个结果时或者开头:

Now begin joins together several expressions into one and the result of a begin block is it's last expression, thus you can even use it in the predicate. Cond has an explicit begin in every consequent so I often switch from if to cond when I either need more than one consequent or a begin:

(cond (predicate-expression  consequent-side-effect-expression 
                             consequent-tail-expression)

      (predicate2-expression consequent2-tail-expression2)

      (else alternative-side-effect-expression 
            alternative-tail-expression))

通常当有副作用时,你并不总是需要替代品。例如。你实际上对 if 返回的内容不感兴趣,因为它也可能不在尾部位置。在这些情况下,您在(rnrs control(6))时有除非带显式开头:

Often when there is side effects you not always need the alternative. Eg. you are actually not interested in what the if returns because it may not be in tail position either. In these cases you have in the (rnrs control (6)) library when and unless with explicit begin:

(when expression
      consequent-side-effect-expression
      ...
      consequent-tail-expression)

(unless expression
        consequent-side-effect-expression
        ...
        consequent-tail-expression)

A 或一个程序也有明确的开头:

A let or a procedure also has explicit begin:

(let ()
  (display "hello") ; displays "hello"
  5)                ; returns 5

(define (test x)
  (display x)       ; display x
  (+ x 5))          ; return x+5

这篇关于“if”的方案中的等价性是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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