[CM] need macro help!
Iain Duncan
iainduncanlists at gmail.com
Wed Sep 8 11:17:41 PDT 2021
oh wow, ok I will chew on that. In the meantime, I got it working as this,
but feedback for style (or bugs I can't see) would be lovely.
;building on the example from Bill:
(define s4m-expr-inputs (vector '() 11 22 33))
(define (string->sexp text)
"convert a string of an sexp to an sexp"
(read (open-input-string text)))
; recursive iterator for processing an sexp for %X args
(define (s4m-process-sexp sexp)
(map
(lambda(token)
(cond
((and (symbol? token) (eq? ((symbol->string token) 0) #\%))
`(s4m-expr-inputs ,(string->number (substring (symbol->string
token) 1))))
((list? token)
(s4m-process-sexp token))
(else token)))
sexp))
(define-macro (s4m-run-sexp-macro sexp)
`(eval (s4m-process-sexp ,sexp)))
(define (s4m-run-expr sexp-str)
(let ((input-sexp (string->sexp sexp-str)))
(s4m-run-sexp-macro input-sexp)))
I also noticed I could thus dispense with the oneline macro all together
now by doing:
(define (s4m-run-expr sexp-str)
(let ((input-sexp (string->sexp sexp-str)))
(eval '(eval (s4m-process-sexp input-sexp)))))
Feedback on which is considered better style welcome!
iain
On Wed, Sep 8, 2021 at 11:03 AM <bil at ccrma.stanford.edu> wrote:
> This isn't helpful, in fact it's ugly, but I can't
> resist mentioning:
>
> (set! *#readers*
> (cons (cons #\% (lambda (str)
> (list 's4m-expr-inputs
> (string->number (substring str 1)))))
> *#readers*))
>
> > (+ #%1 (+ #%2 #%3))
>
> which returns 66 after expanding at read time to:
>
> (+ (s4m-expr-inputs 1) (+ (s4m-expr-inputs 2) (s4m-expr-inputs 3)))
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cm-mail.stanford.edu/pipermail/cmdist/attachments/20210908/37e0df3e/attachment.html>
More information about the Cmdist
mailing list