Is there a read time eval macro like in common lisp?
Yeah, see make-readtable
(https://docs.racket-lang.org/reference/readtables.html#%28def._%28%28quote._~23~25kernel%29._make-readtable%29%29). You need to create a new #lang
for this to work though.
edited: you meant that macro specifically? That looks like quasiquote
and unquote
to me, so change '
to backtick, and change #.
to ,
They are not the same
quasiquote evaluates the expressions at run time, but #. evaluates the expression at read time
Ah, got it
Maybe it can’t be done in Racket because of context stuff
Perhaps:
#lang racket
(require syntax/parse/define)
(define-syntax-parser at-compile
[(_ x) #`'#,(eval-syntax #'x)])
(for ([x 10])
(println `(a ,(at-compile (for/list ([y 10]) (random 20))))))
?
But that is a regular macro right?
The book is talking about a reader macro
But that macro is interesting, I will save it :D
The racket reader only supports “read tables” not “reader macros” in the Common Lisp sense.
reader tables like a set of reader macros?
Maybe you could write a reader macro that recognizes #.
, reads the following expression and wrap it in sorawee’s at-compile macro?
Here’s another solution using let-syntax
, which seems a bit nicer:
#lang racket
(require syntax/parse/define)
(define-syntax-parser at-compile
[(_ x) #'(let-syntax ([trans (λ (_) #`'#,x)])
(trans))])
(for ([x 10])
(println `(a ,(at-compile (cons (random)
(current-seconds))))))
But yeah, what @soegaard2 said, you can just turn #.
to at-compile
Thank you!
Here’s what I wanted to write, but cannot: (define (activity->pattern-dates a)
(define ls-of-ps (hash.refs a.activity_patterns '()))
(if (null? ls-of-ps)
""
(begin
(define ps (car ls-of-ps))
(define ls (hash.refs ps.pattern_dates '()))
ls)))
The problem I believe is that begin
is at where an expression is expected so its form (begin expr ..+)
is used. And in this form I cannot put defines in there. What is the typical solution here? Thank you!
You can use cond
or you can replace begin
with let ()
Wow. I forgot (or didn’t know) that let
could house define
s. Wonderful. Thank you!
what advantages does BSL give to students? and what does it prevent students from doing wrong? trying to teach my little brother and i notice when i hover over variables, there are no pointy arrow things like you get in #lang racket