laurent.orseau
2022-3-29 07:19:54

Awesome, thanks!


joshuahoeflich2021
2022-3-29 16:36:22

Consider the following macro: (define-syntax-rule (adds-new-bindings x) (begin (define x 3))) If I do: (adds-new-bindings foo) This appears to set foo to 3, as I’d expect. However, suppose I want adds-new-bindings to introduce another binding x-baz with a value 4, where x-baz comes from adding -baz to the identifier passed in. In other words, I’d get the following behavior: (adds-new-bindings foo) foo ;; 3 foo-baz ;; 4 How could I go about accomplishing this?


sorawee
2022-3-29 16:38:51

(require syntax/parse/define (for-syntax racket/syntax)) (define-syntax-parse-rule (adds-new-bindings x) #:with new-id (format-id #'x "~a-baz" #'x) (begin (define x 3) (define new-id 4))) (adds-new-bindings foo) foo ;=> 3 foo-baz ;=> 4


joshuahoeflich2021
2022-3-29 16:39:26

Perfect, thank you so much!


ben.knoble
2022-3-29 17:27:16

I would probably have written (define-syntax-parse-rule (adds-new-bindings x:id) …) but this is essentially what I was going to suggest.


notjack
2022-3-30 01:56:13

Also, use (format-id #'x "~a-baz" #'x #:subs? #true) to get identifier renaming and binding arrows to work correctly.


joshuahoeflich2021
2022-3-30 01:58:20

Thank you for the advice, this is great