【发布时间】:2019-09-14 17:01:45
【问题描述】:
在自定义 Racket 语言中,我想更改核心表单 if 以及扩展到它的其他表单(例如 and 和 cond)的行为。
当然,我可以重新定义每种形式,但这似乎是多余的。例如,这是一个示例,其中修改后的if 期望其每个参数都包含在一个列表中。宏 and 在这里被明确地重新定义。
;; my-lang.rkt
#lang racket/base
(require (for-syntax racket/base))
(provide #%module-begin #%datum #%app
list
(rename-out [car-if if] [car-and and]))
(define-syntax (car-if stx)
(syntax-case stx ()
[(_ c t f) #'(if (car c) t f)]))
(define-syntax (car-and stx) ; this seems redundant
(syntax-case stx ()
[(_) #'#t]
[(_ x) #'x]
[(_ x xs ...) #'(car-if x (car-and xs ...) x)]))
#lang s-exp "my-lang.rkt"
(if (list #f) (list 2) (list 3)) ; => (3)
(and (list #f) (list 2)) ; => (#f)
有没有更简单的方法来重新定义这些表单,将我对if 的新定义注入到racket/base 提供的现有定义中?
【问题讨论】: