有没有办法在Racket或任何其他Scheme中定义编译时(扩展时)宏变量?

Mat*_*att 5 macros scheme define-syntax racket

举个简单的例子:

(define-macro-variable _iota 0) ; define-macro-variable does not really exist

(define-syntax (iota stx)
  (syntax-case stx ()
    ((iota)
     (let ((i _iota))
       (set! _iota (+ i 1))
       #`#,i))))
Run Code Online (Sandbox Code Playgroud)

这样给出:

(define zero (iota))
(define one-two-three (list (iota) (iota) (iota)))
(define (four) (iota))
Run Code Online (Sandbox Code Playgroud)

以下都应评估为#t:

(equal? zero 0)
(equal? one-two-three '(1 2 3)) ; possibly in a different order
(equal? (four) 4)
(equal? (four) 4)
(equal? (four) 4)
Run Code Online (Sandbox Code Playgroud)

是否有任何真正的球拍功能define-macro-variable可以完成上述示例中应该执行的操作?

编辑:

我找到了解决办法:

(define-syntaxes (macro-names ...)
  (let (macro-vars-and-vals ...)
    (values macro-bodies-that-nead-the-macro-vars ...)))
Run Code Online (Sandbox Code Playgroud)

但我更喜欢一种解决方案,它不需要使用宏变量的所有宏都在一个表达式中.

stc*_*ang 9

你想要define-for-syntax(在Racket中).

(define-for-syntax _iota 0)

(define-syntax (iota stx)
  (syntax-case stx ()
    ((iota)
     (let ((i _iota))
       (set! _iota (+ i 1))
       #`#,i))))

(define zero (iota))
(define one-two-three (list (iota) (iota) (iota)))
(define (four) (iota))

(equal? zero 0)
(equal? one-two-three '(1 2 3))
(equal? (four) 4)
(equal? (four) 4)
(equal? (four) 4)
Run Code Online (Sandbox Code Playgroud)

产生一切真实.