“条件",“和"和“或"在方案中 [英] "cond","and" and "or" in Scheme

查看:41
本文介绍了“条件",“和"和“或"在方案中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读The Little Schemer.多亏了我蹩脚的英语,这一段让我很困惑:

I'm reading The Little Schemer. And thanks to my broken English, I was confused by this paragraph:

(cond ... ) 还具有不考虑其所有论据.然而,由于这个性质,(和 ... )或(或 ... ) 可以定义为 (cond ... ) 方面的函数,尽管(和...)和(或...)都可以表示为的缩写(cond ... )-表达式:

(cond ... ) also has the property of not considering all of its arguments. Because of this property, however, neither (and ... ) nor (or ... ) can be defined as functions in terms of (cond ... ), though both (and ... ) and (or ... ) can be expressed as abbreviations of (cond ... )-expressions:

(and a b) = (cond (a b) (else #f) 
    and 
(or a b) = (cond (a #t) (else (b))

如果我理解正确,它说 (and ...) and (or ...) 可以用 (cond ...) 表达式替换,但不能定义为包含 (cond ...)为什么会这样?它与变体参数有什么关系吗?谢谢.

If I understand it correctly, it says (and ...) and (or ...) can be replaced by a (cond ...) expression, but cannot be defined as a function that contains (cond ...). Why is it so? Does it have anything to do with the variant arguments? Thanks.

附言我做了一些搜索,但只发现 (cond ...) 在其条件之一评估为 #f 时忽略表达式.

p.s. I did some searching but only found that (cond ...) ignores the expressions when one of its conditions evaluate to #f.

推荐答案

假设您将 if 编写为函数/过程而不是用户定义的宏/语法:

Imagine you wrote if as a function/procedure rather than a user defined macro/syntax:

;; makes if in terms of cond
(define (my-if predicate consequent alternative)
  (cond (predicate consequent)
        (else alternative)))

;; example that works
(define (atom? x)
  (my-if (not (pair? x))
         #t
         #f))

;; example that won't work
;; peano arithemtic
(define (add a b)
  (my-if (zero? a)
         b
         (add (- a 1) (+ b 1))))

my-if 的问题在于,作为过程,每个参数在过程体执行之前都会被评估.因此在 atom? 部分 (not (pair? x))#t#f 被评估在 my-if 的主体被执行之前.

The problem with my-if is that as a procedure every argument gets evaluated before the procedure body gets executed. thus in atom? the parts (not (pair? x)), #t and #f were evaluated before the body of my-if gets executed.

对于最后一个例子意味着 (add (- a 1) (+ b 1)) 被评估而不管 a 是什么,即使 a 为零,所以这个过程永远不会结束.

For the last example means (add (- a 1) (+ b 1)) gets evaluated regardless of what a is, even when a is zero, so the procedure will never end.

您可以使用语法创建自己的 if:

You can make your own if with syntax:

(define-syntax my-if
  (syntax-rules ()
    ((my-if predicate consequent alternative)
     (cond (predicate consequent)
           (else alternative)))))

现在,您如何阅读本文的第一部分是一个模板,其中谓词 consequent 和 Alternative 表示未评估的表达式.它被替换为另一个只是重用表达式,以便:

Now, how you read this is the first part is a template where the predicate consequent and alternative represent unevaluated expressions. It's replaced with the other just reusing the expressions so that:

(my-if (check-something) (display 10) (display 20))

将被替换为:

(cond ((check-something) (display 10))
      (else (display 20)))

使用 my-if 的过程版本,10 和 20 都会被打印出来.这也是 andor 的实现方式.

With the procedure version of my-if both 10 and 20 would have been printed. This is how and and or is implemented as well.

这篇关于“条件",“和"和“或"在方案中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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