【问题标题】:Is there any way to define a compile-time (expansion-time) macro variable in Racket or any other Scheme?有没有办法在 Racket 或任何其他方案中定义编译时(扩展时)宏变量?
【发布时间】:2013-08-16 19:38:10
【问题描述】:

举个简单的例子:

(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))))

这样给定:

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

以下内容均应评估为#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)

是否有任何真正的球拍功能可以完成上面示例中 define-macro-variable 应该做的事情?

编辑:

我找到了解决方法:

(define-syntaxes (macro-names ...)
  (let (macro-vars-and-vals ...)
    (values macro-bodies-that-nead-the-macro-vars ...)))

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

【问题讨论】:

    标签: macros scheme racket define-syntax


    【解决方案1】:

    你想要define-for-syntax(在球拍中)。

    (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)
    

    产生所有真实。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      • 2010-09-11
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      相关资源
      最近更新 更多