在Racket中的嵌套宏中取消引用

时间:2015-06-25 17:09:47

标签: macros racket

上下文

语言为#lang web-server/insta,此代码将在传递给response/xexpr

的quasiquoted参数内生成

电话

-> (define home 'home-page)
-> (make-navigation home "home-page")

当前输出

注意((href , (embed/url ...之前的`backquote

'(p ((id "home-page")) (a `((href ,(embed/url home-page))) "home-page"))

期望输出

请注意,`已经消失了。

'(p ((id "home-page")) (a ((href ,(embed/url home-page))) "home-page"))

当前代码

(define (make-navigation uri label)

  (define (make-id uri)
    `((id ,(symbol->string uri))))

  (define-syntax-rule (embed uri)
    ``((href ,(embed/url ,uri))))

  `(p ,(make-id uri)
      (a ,(embed uri)
     ,label)))

问题轨迹

问题可能出在embed

1 个答案:

答案 0 :(得分:1)

#lang racket
(define (make-navigation uri label)

  (define (make-id uri)
    `((id ,(symbol->string uri))))

  (define-syntax-rule (embed uri)
    '((href ,(embed/url uri))))

  `(p ,(make-id uri)
      (a ,(embed uri)
     ,label)))

(define home 'home-page)
(make-navigation home "home-page")

输出:

'(p ((id "home-page")) (a ((href ,(embed/url uri))) "home-page"))

更新

(define (make-navigation uri label)

  (define (make-id uri)
    `((id ,(symbol->string uri))))

  (define-syntax-rule (embed uri)
    `((href ,(list 'unquote (list 'embed/url uri)))))

  `(p ,(make-id uri)
      (a ,(embed uri)
     ,label)))

(define home 'home-page)
(make-navigation home "home-page")

输出:

'(p ((id "home-page")) (a ((href ,(embed/url home-page))) "home-page"))