Macros 语法中的语法引号、模板变量和省略号

Macros 语法中的语法引号、模板变量和省略号,macros,scheme,racket,Macros,Scheme,Racket,我希望能够做的是转变 (define count-suits (symbol-map-function hearts diamonds clubs spades)) 进入 我有lambda的尸体在处理 (define-syntax (generate-symbol-map stx) (syntax-case stx () ((gen...map enumerations ...) #'(make-hash (cons (quote enumerations) enumer

我希望能够做的是转变

(define count-suits (symbol-map-function hearts diamonds clubs 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 ...)))))
(列表(syntax-e(格式id:~a)(syntax-e#'符号))…)
旨在产生

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

但有人告诉我在#符号之后缺少省略号。我尝试过以不同的方式处理代码,但没有任何真正的目的或见解,我也没有遇到任何有效的方法。

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

在此之后,您可能需要使用,它将语法对象转换为语法对象列表

此外,您不能使用来生成关键字,因为
格式id
将强制将结果作为符号,并将生成的id包含在管道中:

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

下面是
符号映射函数的一个工作实现

(require (for-syntax racket/list))
(define-syntax (symbol-map-function stx)
  (define (id->keyword id)
    (datum->syntax id (string->keyword (symbol->string (syntax-e id)))))
  (syntax-case stx ()
    ((_ id ...)
     (andmap identifier? (syntax->list #'(id ...)))
     (with-syntax ((lambda-list (append-map (lambda (id)
                                              (list (id->keyword id) id))
                                            (syntax->list #'(id ...)))))
       #'(lambda lambda-list
           (make-hash `((id . ,id) ...)))))))
我希望我知道一种比使用
append map
更好的方法来组装lambda列表;欢迎改进。:-)

#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)
(require (for-syntax racket/list))
(define-syntax (symbol-map-function stx)
  (define (id->keyword id)
    (datum->syntax id (string->keyword (symbol->string (syntax-e id)))))
  (syntax-case stx ()
    ((_ id ...)
     (andmap identifier? (syntax->list #'(id ...)))
     (with-syntax ((lambda-list (append-map (lambda (id)
                                              (list (id->keyword id) id))
                                            (syntax->list #'(id ...)))))
       #'(lambda lambda-list
           (make-hash `((id . ,id) ...)))))))