From 5bdc68d7382a054b62472cc7188e9dc3fa229de4 Mon Sep 17 00:00:00 2001 From: Greg Hendershott Date: Thu, 3 Apr 2014 16:52:45 -0400 Subject: [PATCH] Fix mismatch in examples. Fixes #7. --- Transform_.html | 4 ++-- all.html | 6 +++--- index.html | 2 +- index.rkt | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Transform_.html b/Transform_.html index 7fde49a..51315ec 100644 --- a/Transform_.html +++ b/Transform_.html @@ -62,8 +62,8 @@ transformer can rearrange the syntax – rewrite the code – at compile time. The pieces of syntax are moved around, but they aren’t actually evaluated until run time.

Here is one way to do this:

> (define-syntax (our-if-v2 stx)
    (define xs (syntax->list stx))
    (datum->syntax stx `(cond [,(cadr xs) ,(caddr xs)]
                              [else ,(cadddr xs)])))
> (our-if-v2 #t
             (display-and-return "true")
             (display-and-return "false"))

true

"true"

> (our-if-v2 #f
             (display-and-return "true")
             (display-and-return "false"))

false

"false"

That gave the right answer. But how? Let’s pull out the transformer function itself, and see what it did. We start with an example of some -input syntax:

> (define stx #'(our-if-v2 #t "true" "false"))
> (displayln stx)

#<syntax:32:0 (our-if-v2 #t "true" "false")>

1. We take the original syntax, and use syntax->datum to -change it into a plain Racket list:

> (define xs (syntax->datum stx))
> (displayln xs)

(our-if-v2 #t true false)

2. To change this into a Racket cond form, we need to take +input syntax:

> (define stx #'(our-if-v2 #t "true" "false"))
> (displayln stx)

#<syntax:32:0 (our-if-v2 #t "true" "false")>

1. We take the original syntax, and use syntax->list to +change it into a list of syntax objects:

> (define xs (syntax->list stx))
> (displayln xs)

(#<syntax:32:0 our-if-v2> #<syntax:32:0 #t> #<syntax:32:0 "true"> #<syntax:32:0 "false">)

2. To change this into a Racket cond form, we need to take the three interesting pieces—the condition, true-expression, and false-expression—from the list using cadr, caddr, and cadddr and arrange them into a cond form:

`(cond [,(cadr xs) ,(caddr xs)]
       [else ,(cadddr xs)])

3. Finally, we change that into syntax using diff --git a/all.html b/all.html index ec58f7a..b992191 100644 --- a/all.html +++ b/all.html @@ -1,6 +1,6 @@ Fear of Macros

Fear of Macros

-
Copyright (c) 2012-2014 by Greg Hendershott. All rights reserved.
Last updated 2014-03-25T08:52:41
Feedback and corrections are welcome here.

Contents:

    1 Preface

    2 Our plan of attack

    3 Transform!

      3.1 What is a syntax transformer?

      3.2 What’s the input?

      3.3 Actually transforming the input

      3.4 Compile time vs. run time

      3.5 begin-for-syntax

    4 Pattern matching: syntax-case and syntax-rules

      4.1 Pattern variable vs. template—fight!

        4.1.1 with-syntax

        4.1.2 with-syntax*

        4.1.3 format-id

        4.1.4 Another example

      4.2 Making our own struct

      4.3 Using dot notation for nested hash lookups

    5 Syntax parameters

    6 What’s the point of splicing-let?

    7 Robust macros: syntax-parse

      7.1 Error-handling strategies for functions

      7.2 Error-handling strategies for macros

      7.3 Using syntax-parse

    8 References and Acknowledgments

    9 Epilogue

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 +

Copyright (c) 2012-2014 by Greg Hendershott. All rights reserved.
Last updated 2014-04-03T16:51:14
Feedback and corrections are welcome here.

Contents:

    1 Preface

    2 Our plan of attack

    3 Transform!

      3.1 What is a syntax transformer?

      3.2 What’s the input?

      3.3 Actually transforming the input

      3.4 Compile time vs. run time

      3.5 begin-for-syntax

    4 Pattern matching: syntax-case and syntax-rules

      4.1 Pattern variable vs. template—fight!

        4.1.1 with-syntax

        4.1.2 with-syntax*

        4.1.3 format-id

        4.1.4 Another example

      4.2 Making our own struct

      4.3 Using dot notation for nested hash lookups

    5 Syntax parameters

    6 What’s the point of splicing-let?

    7 Robust macros: syntax-parse

      7.1 Error-handling strategies for functions

      7.2 Error-handling strategies for macros

      7.3 Using syntax-parse

    8 References and Acknowledgments

    9 Epilogue

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 @@ -105,8 +105,8 @@ transformer can rearrange the syntax – rewrite the code – at compile time. The pieces of syntax are moved around, but they aren’t actually evaluated until run time.

Here is one way to do this:

> (define-syntax (our-if-v2 stx)
    (define xs (syntax->list stx))
    (datum->syntax stx `(cond [,(cadr xs) ,(caddr xs)]
                              [else ,(cadddr xs)])))
> (our-if-v2 #t
             (display-and-return "true")
             (display-and-return "false"))

true

"true"

> (our-if-v2 #f
             (display-and-return "true")
             (display-and-return "false"))

false

"false"

That gave the right answer. But how? Let’s pull out the transformer function itself, and see what it did. We start with an example of some -input syntax:

> (define stx #'(our-if-v2 #t "true" "false"))
> (displayln stx)

#<syntax:32:0 (our-if-v2 #t "true" "false")>

1. We take the original syntax, and use syntax->datum to -change it into a plain Racket list:

> (define xs (syntax->datum stx))
> (displayln xs)

(our-if-v2 #t true false)

2. To change this into a Racket cond form, we need to take +input syntax:

> (define stx #'(our-if-v2 #t "true" "false"))
> (displayln stx)

#<syntax:32:0 (our-if-v2 #t "true" "false")>

1. We take the original syntax, and use syntax->list to +change it into a list of syntax objects:

> (define xs (syntax->list stx))
> (displayln xs)

(#<syntax:32:0 our-if-v2> #<syntax:32:0 #t> #<syntax:32:0 "true"> #<syntax:32:0 "false">)

2. To change this into a Racket cond form, we need to take the three interesting pieces—the condition, true-expression, and false-expression—from the list using cadr, caddr, and cadddr and arrange them into a cond form:

`(cond [,(cadr xs) ,(caddr xs)]
       [else ,(cadddr xs)])

3. Finally, we change that into syntax using diff --git a/index.html b/index.html index 6582f28..49fad5a 100644 --- a/index.html +++ b/index.html @@ -1,3 +1,3 @@ Fear of Macros

Fear of Macros

-
Copyright (c) 2012-2014 by Greg Hendershott. All rights reserved.
Last updated 2014-03-25T08:52:33
Feedback and corrections are welcome here.

Contents:

    1 Preface

    2 Our plan of attack

    3 Transform!

      3.1 What is a syntax transformer?

      3.2 What’s the input?

      3.3 Actually transforming the input

      3.4 Compile time vs. run time

      3.5 begin-for-syntax

    4 Pattern matching: syntax-case and syntax-rules

      4.1 Pattern variable vs. template—fight!

        4.1.1 with-syntax

        4.1.2 with-syntax*

        4.1.3 format-id

        4.1.4 Another example

      4.2 Making our own struct

      4.3 Using dot notation for nested hash lookups

    5 Syntax parameters

    6 What’s the point of splicing-let?

    7 Robust macros: syntax-parse

      7.1 Error-handling strategies for functions

      7.2 Error-handling strategies for macros

      7.3 Using syntax-parse

    8 References and Acknowledgments

    9 Epilogue

 
\ No newline at end of file +
Copyright (c) 2012-2014 by Greg Hendershott. All rights reserved.
Last updated 2014-04-03T16:51:05
Feedback and corrections are welcome here.

Contents:

    1 Preface

    2 Our plan of attack

    3 Transform!

      3.1 What is a syntax transformer?

      3.2 What’s the input?

      3.3 Actually transforming the input

      3.4 Compile time vs. run time

      3.5 begin-for-syntax

    4 Pattern matching: syntax-case and syntax-rules

      4.1 Pattern variable vs. template—fight!

        4.1.1 with-syntax

        4.1.2 with-syntax*

        4.1.3 format-id

        4.1.4 Another example

      4.2 Making our own struct

      4.3 Using dot notation for nested hash lookups

    5 Syntax parameters

    6 What’s the point of splicing-let?

    7 Robust macros: syntax-parse

      7.1 Error-handling strategies for functions

      7.2 Error-handling strategies for macros

      7.3 Using syntax-parse

    8 References and Acknowledgments

    9 Epilogue

 
\ No newline at end of file diff --git a/index.rkt b/index.rkt index da74910..cc74011 100644 --- a/index.rkt +++ b/index.rkt @@ -447,11 +447,11 @@ input syntax: (displayln stx) ] -1. We take the original syntax, and use @racket[syntax->datum] to -change it into a plain Racket @racket[list]: +1. We take the original syntax, and use @racket[syntax->list] to +change it into a @racket[list] of syntax objects: @i[ -(define xs (syntax->datum stx)) +(define xs (syntax->list stx)) (displayln xs) ]