From 65e3eca65ebe020c30e2830bf8702328a870400a Mon Sep 17 00:00:00 2001 From: Greg Hendershott Date: Thu, 25 Oct 2012 18:56:18 -0400 Subject: [PATCH] Insert obligatory "Ceci n'est pas une pipe" joke. --- index.html | 4 ++-- main.rkt | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 75cab9f..c833dfc 100644 --- a/index.html +++ b/index.html @@ -2,7 +2,7 @@ Fear of Macros

Fear of Macros

Copyright (c) 2012 by Greg Hendershott. All rights reserved. -
Last updated 2012-10-25 18:15:21

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 +

Last updated 2012-10-25 18:54:58

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