Fear of Macros

Contents:
1 Preface
I learned Racket after 25 years of mostly using C and C++.
Some psychic whiplash resulted.
"All the parentheses" was actually not a big deal. Instead, the first +
Contents:
1 Preface
I learned Racket after 25 years of mostly using C and C++.
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
@@ -79,7 +79,9 @@ and so on recursively— When we want to transform syntax, we’ll generally take the pieces we
were given, maybe rearrange their order, perhaps change some of the
pieces, and often introduce brand-new pieces. Let’s write a transformer function that reverses the syntax it was
-given: "i" "am" "backwards" Understand Yoda, can we. Great, but how does this work? First we take the input syntax, and give it to
+given: the values at the end allows the result to
+evaluate nicely. Try (reverse-me "backwards" "am" "i") to
+see why it’s handy. "i" "am" "backwards"3.3 Actually transforming the input
> (define-syntax (reverse-me stx) (datum->syntax stx (reverse (cdr (syntax->datum stx))))) > (reverse-me "backwards" "am" "i" values) > (define-syntax (reverse-me stx) (datum->syntax stx (reverse (cdr (syntax->datum stx))))) > (reverse-me "backwards" "am" "i" values)
Understand Yoda, can we. Great, but how does this work?
First we take the input syntax, and give it to syntax->datum. This converts the syntax into a plain old list:
> (syntax->datum #'(reverse-me "backwards" "am" "i" values)) '(reverse-me "backwards" "am" "i" values)
Using cdr slices off the first item of the list, reverse-me, leaving the remainder: diff --git a/index.html b/index.html index a4da1b7..aebe792 100644 --- a/index.html +++ b/index.html @@ -1,3 +1,3 @@
Fear of Macros

Contents: