打印变量名及其值的列表



我已经有了这个宏:

(define-syntax inspect
  (syntax-rules ()
    [(_ x) (printf "~a is: ~an" 'x x)]))

工作原理如下:

(let ([x 2]) (inspect x))
>> x is: 2

但是我想做的是扩展它,这样我就有了

(_ x ...)

和循环遍历提供的各种值,相应地打印出它们的变量名和值。

ie

  (let ([x 2] [y 3]) (inspect x y))
    >> x is: 2
    y is: 3

这部分我有点卡住了。

例如:

(define-syntax (inspect stx)
  (datum->syntax
   stx
   (for ([i (cdr (syntax->list stx))])
     (printf "~a is: ~a" (syntax->datum i) i))))

我不确定如何得到I的值,在最后一行。

任何帮助都是感激的。

谢谢。

没有必要使用双模式规则——您可以使用begin:

完成所有操作。
(define-syntax-rule (inspect x ...)
  (begin (printf "~a is: ~an" 'x x) ...))

这里有一个解决方案:

(define-syntax inspect
  (syntax-rules ()
    [(_ x) (printf "~a is: ~an" 'x x)]
    [(_ x y ...) (begin (inspect x) (inspect y ...))]))

注意子句的顺序很重要

最新更新