【问题标题】:What is the equivalence in scheme for 'if'? [closed]'if' 在方案中的等价性是什么? [关闭]
【发布时间】:2013-11-24 19:30:30
【问题描述】:

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

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

谢谢。

【问题讨论】:

  • 这个问题似乎是题外话,因为它是关于一个基本的编程控制结构(if/then/else)。
  • 这是非常基础的知识,有很多关于它的资源。请随意阅读 SICP,它应该可以回答您的大部分问题。

标签: if-statement scheme


【解决方案1】:

代码对每个真假分支执行一个块的并行代码是:

   (if (condition)
       (begin
          (function1)
          (function2))
       (begin
          (function3)
          (function4)))

【讨论】:

  • 这是迄今为止解决 then 和 else 部分中的多个表达式问题的唯一答案。
【解决方案2】:

这取决于。您确实应该尝试通过方案中的实际问题进一步缩小问题范围,因为答案将取决于您要做什么。

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

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

但是对于您的代码,您在分支中拥有不止一件事,例如:

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

这里,为了让function1function3 做任何有用的事情,它必须变异、读取或写入某些东西,也称为副作用。您应该尽量避免这种情况,因此在 Scheme 中您可以使用 let 来存储临时变量,进行调用,以便在它返回时像这样使用它:

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

现在。您最终将需要一些过程的副作用,然后您需要使用begin 或使用具有显式开始的cond

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

现在begin 将几个表达式合并为一个,begin 块的结果是它的最后一个表达式,因此您甚至可以在谓词中使用它。 Cond 在每个结果中都有一个明确的开始,所以当我需要多个结果或开始时,我经常从if 切换到cond

(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)) 库中的 whenunless 具有显式开头:

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

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

letprocedure 也有明确的开头:

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

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

【讨论】:

    【解决方案3】:

    怎么样

    (if condition
        then-code
        else-code)
    

    方案也有更通用的

    (cond
      ((test1) case-1)
      ((test2) case-2)
      ...
      (else else-case))
    

    如果你使用的是 Racket,那么

    (when test then-code)
    

    if,没有else。

    【讨论】:

      【解决方案4】:

      有几个Intro to Scheme 网站涵盖了这类事情。以下是链接中的一个示例:

      (define (min a b)
         (if (< a b)
             a
             b))
      

      【讨论】:

        【解决方案5】:

        WorBlux replied in another thread:

        (if 条件 1 (开始函数 1 函数 2) (开始函数 3 function4)) 'begin' 是一个强制顺序的过程/宏 从左到右评估其每个参数,并返回 评估的最后一个参数的值。在 cond 形式中的每个子句 被包裹在一个隐式的开始中。定义特殊形式也是 如此包装,所以你可以做 (cond (condition1 function1 function2) (else function3 function4)) – WorBlux

        【讨论】:

          猜你喜欢
          • 2013-12-16
          • 2016-07-04
          • 2012-09-06
          • 1970-01-01
          • 2012-09-12
          • 2010-11-16
          • 2012-12-29
          • 1970-01-01
          • 2014-02-24
          相关资源
          最近更新 更多