Macros Racket宏将s表达式以外的所有表达式字符串化
我试图弄清楚如何创建一个racket宏,它将把所有内容转换成字符串,但s表达式除外,它将不处理这些内容 我已经弄清楚了如何把每件事都严格化Macros Racket宏将s表达式以外的所有表达式字符串化,macros,racket,Macros,Racket,我试图弄清楚如何创建一个racket宏,它将把所有内容转换成字符串,但s表达式除外,它将不处理这些内容 我已经弄清楚了如何把每件事都严格化 #+begin_src racket (define-syntax (stringify-all stx) (syntax-case stx () [(_ args ...) #'(begin (~s 'args) ...)])) (str
#+begin_src racket
(define-syntax (stringify-all stx)
(syntax-case stx ()
[(_ args ...)
#'(begin
(~s 'args)
...)]))
(stringify-all one 2 (add1 2))
#+end_src
#+RESULTS:
: "one"
: "2"
: "(add1 2)"
但是,我不知道如何测试某个东西是否是s表达式
如果我们使用原子的通常定义,我该怎么做?参考程序:
(defne (atom? exp)
(and (not (null? exp))
(not (pair? exp))))
然后我们可以将s表达式谓词定义为:
(define (s-exp? exp)
(or (null? exp)
(pair? exp)))
当然,你需要将它递归地应用到s表达式的每个元素上。如果我们使用原子的通常定义?参考程序:
(defne (atom? exp)
(and (not (null? exp))
(not (pair? exp))))
然后我们可以将s表达式谓词定义为:
(define (s-exp? exp)
(or (null? exp)
(pair? exp)))
当然,您需要将其递归地应用于s表达式的每个元素。s表达式是语法列表/对,因此您可以对和进行模式匹配。b、 像这样:
#lang racket
(require (for-syntax (only-in racket/format ~s)))
(define-for-syntax (compute stx)
(syntax-case stx ()
[() #'null]
[(a . b) #`(cons #,(compute #'a) #,(compute #'b))]
[_ (~s (syntax-e stx))]))
(define-syntax (stringify-all stx)
(syntax-case stx ()
[(_ args ...)
#`(begin #,@(map compute (syntax->list #'(args ...))))]))
(stringify-all one 2 (add1 2))
;; "one"
;; "2"
;; '("add1" "2")
s-expression是语法列表/对,因此您可以对和进行模式匹配。b、 像这样:
#lang racket
(require (for-syntax (only-in racket/format ~s)))
(define-for-syntax (compute stx)
(syntax-case stx ()
[() #'null]
[(a . b) #`(cons #,(compute #'a) #,(compute #'b))]
[_ (~s (syntax-e stx))]))
(define-syntax (stringify-all stx)
(syntax-case stx ()
[(_ args ...)
#`(begin #,@(map compute (syntax->list #'(args ...))))]))
(stringify-all one 2 (add1 2))
;; "one"
;; "2"
;; '("add1" "2")
啊,谢谢!我只想计算嵌套的s表达式,但我想我能理解这一点。太好了,谢谢。啊,谢谢!我只想计算嵌套的s表达式,但我想我能理解这一点。太好了,谢谢你。