Fear of Macros

1 Introduction
I learned Racket after 25 years of doing C/C++ imperative programming.
Some psychic whiplash resulted.
"All the parentheses" was actually not a big deal. Instead, the first +
1 Introduction
I learned Racket after 25 years of doing C/C++ imperative programming.
Some psychic whiplash resulted.
"All the parentheses" was actually not a big deal. Instead, the first mind warp was functional programming. Before long I wrapped my brain around it, and went on to become comfortable and effective with many other aspects and features of Racket.
But two final frontiers remained: Macros and continuations.
I found that simple macros were easy and understandable, plus there @@ -78,7 +78,7 @@ given:
("backwards" "am" "i" values). Passing that to reverse changes it to (values "i" "am" "backwards"):
> (reverse (cdr '("backwards" "am" "i" values))) '(values "i" "am")
Finally we use syntax->datum to convert this back to syntax:
> (datum->syntax #f '(values "i" "am" "backwards")) #<syntax (values "i" "am" "backwards")>
That’s what our transformer function gives back to the Racket -compiler, and that syntax is evaluated:
> (values "i" "am" "backwards") "i"
"am"
"backwards"
3.4 Compile time vs. run time
Normal Racket code runs at ... run time. Duh.
Instead of "compile time vs. run time", you may hear it +compiler, and that syntax is evaluated:
> (values "i" "am" "backwards") "i"
"am"
"backwards"
3.4 Compile time vs. run time
(define-syntax (foo stx) (make-pipe)) ;This is not run time. Normal Racket code runs at ... run time. Duh.
Instead of "compile time vs. run time", you may hear it described as "syntax phase vs. runtime phase". Same difference.
But a syntax transformer is run by the Racket compiler, as part of the process of parsing, expanding and understanding your code. In other words, your syntax transformer function is evaluated at compile time.
This aspect of macros lets you do things that simply aren’t possible diff --git a/main.rkt b/main.rkt index 8268f3e..1a4c24b 100644 --- a/main.rkt +++ b/main.rkt @@ -287,6 +287,11 @@ compiler, and @italic{that} syntax is evaluated: @subsection{Compile time vs. run time} +@codeblock[#:indent 10]{ +(define-syntax (foo stx) + (make-pipe)) ;This is not run time. +} + Normal Racket code runs at ... run time. Duh. @margin-note{Instead of "compile time vs. run time", you may hear it