gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:36:02 25 4
gpt4 key购买 nike

举个简单的例子:

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

有什么真正的 Racket 功能可以做 define-macro-variable应该在上面的例子中做什么?

编辑:

我找到了一个解决方法:
(define-syntaxes (macro-names ...)
(let (macro-vars-and-vals ...)
(values macro-bodies-that-nead-the-macro-vars ...)))

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

最佳答案

您要 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)

产生所有真实。

关于macros - 有没有办法在 Racket 或任何其他 Scheme 中定义编译时(扩展时)宏变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18238722/

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