gpt4 book ai didi

macros - 在 syntax-case 中绊倒 syntax-quote-unsplicing、template variables 和 ellipses

转载 作者:行者123 更新时间:2023-12-01 11:40:58 34 4
gpt4 key购买 nike

我想做的是转换例如

(define count-suits (symbol-map-function hearts diamonds clubs spades))

进入

(define count-suits (λ (#:hearts hearts
#:diamonds diamonds
#:clubs clubs
#:spades spades)
(make-hash (cons 'hearts hearts)
(cons 'diamonds diamonds)
(cons 'clubs clubs)
(cons 'spades spades))))

我有 lambda 的主体

(define-syntax (generate-symbol-map stx)
(syntax-case stx ()
((gen...map enumerations ...)
#'(make-hash (cons (quote enumerations) enumerations) ...))))

但我的时间很糟糕

(λ (#:hearts hearts
#:diamonds diamonds
#:clubs clubs
#:spades spades)

这是我目前的情况

;; e.g. (weave '(1 3 5 7) '(2 4 6 8)) = '(1 2 3 4 5 6 7 8)
;; tested, works.
(define-for-syntax (weave list1 list2)
(cond ((empty? list1) list2)
((empty? list2) list1)
(else (list* (car list1)
(car list2)
(weave (cdr list1)
(cdr list2))))))

(define-syntax (symbol-map-function stx)
(syntax-case stx ()
((sym...ion symbols ...)
; What I'm trying to do here is splice the result of weaving the keywords,
; generated by format-id, with the symbols themselves, e.g. in the case of
; (symbol-map-function foo bar baz):
; #`(λ (#,@(weave '(#:foo #:bar #:baz) '(foo bar baz)))
; --> #`(λ (#,@'(#:foo foo #:bar bar #:baz baz))
; --> #`(λ (#:foo foo #:bar bar #:baz baz)
; I am using syntax-unquote-splicing because I am in syntax-quasiquote and the
; result of the expression is a list that I want to be spliced into the arguments.
#`(λ (#,@(weave (list (syntax-e (format-id #'symbols
"#:~a"
(syntax-e #'symbols))) ...)
(list #'(symbols ...))))
(generate-symbol-map symbols ...)))))

(list (syntax-e (format-id #'symbols "#:~a"(syntax-e #'symbols))) ...) 是为了导致

(list (syntax-e (format-id #'foo "#:~a" (syntax-e #'foo)))
(syntax-e (format-id #'bar "#:~a" (syntax-e #'bar)))
(syntax-e (format-id #'baz "#:~a" (syntax-e #'baz))))

但有人告诉我在#'符号后缺少省略号。我尝试过以不同的方式使用代码,但没有任何真正的目的或洞察力,而且我还没有偶然发现任何有效的方法。

最佳答案

... 不能出现在模板之外,这意味着它们必须出现在 symbols 之前的 #' 部分内。您可以编写 #'(symbols ...) 但不能编写 #'symbols ...

在此之后,您可能想要使用 syntax->list ,它将您的语法对象变成语法对象列表。

此外,您不能使用 format-id生成关键字,因为 format-id 会将结果强制为一个符号,并将生成的 id 包含在管道中:

> (require racket/syntax)
> (format-id #'here "#:~a" 'auie)
#<syntax |#:auie|>

所以你需要使用syntax->datum , symbol->string , 然后 string->keyword在这里做你想做的事。

这是一个工作示例:

#lang racket
(require (for-syntax racket/syntax racket/list))

(define-syntax (foo stx)
(syntax-case stx ()
[(_ (sym ...) body ...)
(with-syntax ([kws (flatten
(map (λ(k)
(list
(string->keyword
(symbol->string
(syntax->datum k)))
k))
(syntax->list #'(sym ...))))]
)
#'(λ kws body ...))]))

; Test:
((foo (aa bb)
(list aa bb))
#:bb 'bbb
#:aa 'aaa)
; -> '(aaa bbb)

关于macros - 在 syntax-case 中绊倒 syntax-quote-unsplicing、template variables 和 ellipses,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20897292/

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