方案中的条件定义

Juw*_*wan 4 lisp scheme eval conditional-statements racket

我想这将是一个简单的问题,但我需要它。我正在计划中制作模拟器游戏(球拍博士),我想改变 cond 的工作方式。但是要改变 cond 的东西,我需要知道 cond 的定义和我在球拍博士中找不到它。有人可以给出方案中 cond 的定义吗?

Chr*_*ung 6

Racket 的定义condcollects/racket/private/cond.rkt. 它是使用低级语法对象操作编写的,而不是使用syntax-rulesor syntax-case,因此除非您非常了解语法对象,否则您将无法阅读它。

作为您自定义的替代起点cond,其中一个定义condSRFI 61 中给出的参考实现。它很简洁,是cond我见过的最好的实现之一:

(define-syntax cond
  (syntax-rules (=> else)

    ((cond (else else1 else2 ...))
     ;; The (if #t (begin ...)) wrapper ensures that there may be no
     ;; internal definitions in the body of the clause.  R5RS mandates
     ;; this in text (by referring to each subform of the clauses as
     ;; <expression>) but not in its reference implementation of cond,
     ;; which just expands to (begin ...) with no (if #t ...) wrapper.
     (if #t (begin else1 else2 ...)))

    ((cond (test => receiver) more-clause ...)
     (let ((t test))
       (cond/maybe-more t
                        (receiver t)
                        more-clause ...)))

    ((cond (generator guard => receiver) more-clause ...)
     (call-with-values (lambda () generator)
       (lambda t
         (cond/maybe-more (apply guard    t)
                          (apply receiver t)
                          more-clause ...))))

    ((cond (test) more-clause ...)
     (let ((t test))
       (cond/maybe-more t t more-clause ...)))

    ((cond (test body1 body2 ...) more-clause ...)
     (cond/maybe-more test
                      (begin body1 body2 ...)
                      more-clause ...))))

(define-syntax cond/maybe-more
  (syntax-rules ()
    ((cond/maybe-more test consequent)
     (if test
         consequent))
    ((cond/maybe-more test consequent clause ...)
     (if test
         consequent
         (cond clause ...)))))
Run Code Online (Sandbox Code Playgroud)

(不过,正如 molbdnilo 所说,请不要cond将您的版本称为其他名称以避免混淆。)