gpt4 book ai didi

scheme - 方案中的条件定义

转载 作者:太空宇宙 更新时间:2023-11-03 18:42:35 25 4
gpt4 key购买 nike

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

最佳答案

cond的Racket定义在collects/racket/private/cond.rkt中。它是使用低级语法对象操作编写的,既不使用 syntax-rules 也不使用 syntax-case,因此除非您非常了解语法对象,否则它不会可读给你。

作为自定义 cond 的替代起点,cond 的一个定义是 SRFI 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 ...)))))

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

关于scheme - 方案中的条件定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27506628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com