hectometrocuadrado
2021-8-16 09:00:43

Is there a read time eval macro like in common lisp?


sorawee
2021-8-16 09:02:50

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 ,


hectometrocuadrado
2021-8-16 09:05:08

They are not the same


hectometrocuadrado
2021-8-16 09:05:52

quasiquote evaluates the expressions at run time, but #. evaluates the expression at read time


sorawee
2021-8-16 09:06:19

Ah, got it


hectometrocuadrado
2021-8-16 09:07:42

Maybe it can’t be done in Racket because of context stuff


sorawee
2021-8-16 09:15:35

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)))))) ?


hectometrocuadrado
2021-8-16 09:45:03

But that is a regular macro right?


hectometrocuadrado
2021-8-16 09:45:19

The book is talking about a reader macro


hectometrocuadrado
2021-8-16 09:45:50

But that macro is interesting, I will save it :D


soegaard2
2021-8-16 09:47:06

The racket reader only supports “read tables” not “reader macros” in the Common Lisp sense.

https://docs.racket-lang.org/reference/readtables.html


hectometrocuadrado
2021-8-16 09:49:26

reader tables like a set of reader macros?


soegaard2
2021-8-16 09:52:23

Maybe you could write a reader macro that recognizes #. , reads the following expression and wrap it in sorawee’s at-compile macro?


sorawee
2021-8-16 11:29:50

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))))))


sorawee
2021-8-16 11:30:22

But yeah, what @soegaard2 said, you can just turn #. to at-compile


hectometrocuadrado
2021-8-16 12:21:08

Thank you!


anything
2021-8-16 16:35:17

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!


samth
2021-8-16 16:50:00

You can use cond or you can replace begin with let ()


anything
2021-8-16 16:55:21

Wow. I forgot (or didn’t know) that let could house defines. Wonderful. Thank you!


jestarray
2021-8-17 06:07:45

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