Various edits
This commit is contained in:
parent
5c2d224d69
commit
6c5c39e12f
49
main.rkt
49
main.rkt
|
@ -7,7 +7,8 @@
|
||||||
(for-syntax racket/base)
|
(for-syntax racket/base)
|
||||||
(for-label racket)
|
(for-label racket)
|
||||||
(for-label racket/stxparam)
|
(for-label racket/stxparam)
|
||||||
(for-label syntax/parse))
|
(for-label syntax/parse)
|
||||||
|
(for-label racket/splicing))
|
||||||
@(define evaluator
|
@(define evaluator
|
||||||
(parameterize ([sandbox-output 'string]
|
(parameterize ([sandbox-output 'string]
|
||||||
[sandbox-error-output 'string])
|
[sandbox-error-output 'string])
|
||||||
|
@ -16,14 +17,18 @@
|
||||||
@(define-syntax-rule (i body ...)
|
@(define-syntax-rule (i body ...)
|
||||||
(interaction #:eval evaluator body ...))
|
(interaction #:eval evaluator body ...))
|
||||||
|
|
||||||
@image["fear-of-macros.jpg"]
|
|
||||||
@title[#:version ""]{Fear of Macros}
|
@title[#:version ""]{Fear of Macros}
|
||||||
@author[@hyperlink["https://github.com/greghendershott/fear-of-macros/issues"
|
@author[@hyperlink["http://www.greghendershott.com"
|
||||||
"Greg Hendershott"]]
|
"Greg Hendershott"]]
|
||||||
@smaller{Copyright (c) 2012 by Greg Hendershott. All rights reserved.}
|
@image["fear-of-macros.jpg"]
|
||||||
|
@para[@smaller{Copyright (c) 2012 by Greg Hendershott. All rights reserved.}]
|
||||||
@para[@smaller["Last updated "
|
@para[@smaller["Last updated "
|
||||||
(parameterize ([date-display-format 'iso-8601])
|
(parameterize ([date-display-format 'iso-8601])
|
||||||
(date->string (current-date) #t))]]
|
(date->string (current-date) #t))]]
|
||||||
|
@para{Feedback and corrections are @hyperlink["https://github.com/greghendershott/fear-of-macros/issues" "welcome here"].}
|
||||||
|
|
||||||
|
Contents:
|
||||||
|
|
||||||
@table-of-contents{}
|
@table-of-contents{}
|
||||||
|
|
||||||
@; ----------------------------------------------------------------------------
|
@; ----------------------------------------------------------------------------
|
||||||
|
@ -109,6 +114,7 @@ similar for macro. There is. One of the more-recent Racket macro
|
||||||
enhancements is @racket[syntax-parse].
|
enhancements is @racket[syntax-parse].
|
||||||
|
|
||||||
|
|
||||||
|
@; ----------------------------------------------------------------------------
|
||||||
@; ----------------------------------------------------------------------------
|
@; ----------------------------------------------------------------------------
|
||||||
|
|
||||||
@section{Transformers}
|
@section{Transformers}
|
||||||
|
@ -217,6 +223,7 @@ When Racket expands our program, it sees the occurrence of
|
||||||
calls our function with the old syntax, and we return the new syntax,
|
calls our function with the old syntax, and we return the new syntax,
|
||||||
which is used to evaluate and run our program.
|
which is used to evaluate and run our program.
|
||||||
|
|
||||||
|
@; ----------------------------------------------------------------------------
|
||||||
|
|
||||||
@subsection{What's the input?}
|
@subsection{What's the input?}
|
||||||
|
|
||||||
|
@ -298,6 +305,9 @@ When we want to transform syntax, we'll generally take the pieces we
|
||||||
were given, maybe rearrange their order, perhaps change some of the
|
were given, maybe rearrange their order, perhaps change some of the
|
||||||
pieces, and often introduce brand-new pieces.
|
pieces, and often introduce brand-new pieces.
|
||||||
|
|
||||||
|
|
||||||
|
@; ----------------------------------------------------------------------------
|
||||||
|
|
||||||
@subsection{Actually transforming the input}
|
@subsection{Actually transforming the input}
|
||||||
|
|
||||||
Let's write a transformer function that reverses the syntax it was
|
Let's write a transformer function that reverses the syntax it was
|
||||||
|
@ -342,6 +352,7 @@ compiler, and @italic{that} syntax is evaluated:
|
||||||
(values "i" "am" "backwards")
|
(values "i" "am" "backwards")
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@; ----------------------------------------------------------------------------
|
||||||
|
|
||||||
@subsection{Compile time vs. run time}
|
@subsection{Compile time vs. run time}
|
||||||
|
|
||||||
|
@ -509,6 +520,8 @@ So let's try that:
|
||||||
(our-if-using-match-v2 #t "true" "false")
|
(our-if-using-match-v2 #t "true" "false")
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@; ----------------------------------------------------------------------------
|
||||||
|
|
||||||
@subsection{@racket[begin-for-syntax]}
|
@subsection{@racket[begin-for-syntax]}
|
||||||
|
|
||||||
We used @racket[for-syntax] to @racket[require] the
|
We used @racket[for-syntax] to @racket[require] the
|
||||||
|
@ -577,6 +590,7 @@ them available at compile time.}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@; ----------------------------------------------------------------------------
|
||||||
@; ----------------------------------------------------------------------------
|
@; ----------------------------------------------------------------------------
|
||||||
|
|
||||||
@section{Pattern matching: syntax-case and syntax-rules}
|
@section{Pattern matching: syntax-case and syntax-rules}
|
||||||
|
@ -655,6 +669,8 @@ Sometimes, we need to go a step beyond the pattern and template. Let's
|
||||||
look at some examples, how we can get confused, and how to get it
|
look at some examples, how we can get confused, and how to get it
|
||||||
working.
|
working.
|
||||||
|
|
||||||
|
@; ----------------------------------------------------------------------------
|
||||||
|
|
||||||
@subsection{"A pattern variable can't be used outside of a template"}
|
@subsection{"A pattern variable can't be used outside of a template"}
|
||||||
|
|
||||||
Let's say we want to define a function with a hyphenated name, a-b,
|
Let's say we want to define a function with a hyphenated name, a-b,
|
||||||
|
@ -1072,6 +1088,7 @@ it
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@; ----------------------------------------------------------------------------
|
||||||
@; ----------------------------------------------------------------------------
|
@; ----------------------------------------------------------------------------
|
||||||
|
|
||||||
@section{Robust macros: syntax-parse}
|
@section{Robust macros: syntax-parse}
|
||||||
|
@ -1082,14 +1099,17 @@ TO-DO.
|
||||||
|
|
||||||
TO-DO.
|
TO-DO.
|
||||||
|
|
||||||
|
@; ----------------------------------------------------------------------------
|
||||||
@; ----------------------------------------------------------------------------
|
@; ----------------------------------------------------------------------------
|
||||||
|
|
||||||
@section{What's the point of @racket[racket/splicing]?}
|
@section{What's the point of @racket[splicing-let]?}
|
||||||
|
|
||||||
I stared at @racket[racket/splicing] for the longest time, not
|
I stared at @racket[racket/splicing] for the longest time. What does
|
||||||
understanding exactly how it works, or why I'd want to use it. As with
|
it do? Why would I use it? Why is it in the Macros section of the
|
||||||
other aspects of Racket macros, step number one was to de-mythologize
|
reference?
|
||||||
it. This:
|
|
||||||
|
Step one, @elem[#:style "strike"]{cut a hole in the box}
|
||||||
|
de-mythologize it. For example, using @racket[splicing-let] like this:
|
||||||
|
|
||||||
@#reader scribble/comment-reader
|
@#reader scribble/comment-reader
|
||||||
(i
|
(i
|
||||||
|
@ -1103,7 +1123,7 @@ it. This:
|
||||||
x
|
x
|
||||||
)
|
)
|
||||||
|
|
||||||
is shorthand for this:
|
is equivalent to:
|
||||||
|
|
||||||
@#reader scribble/comment-reader
|
@#reader scribble/comment-reader
|
||||||
(i
|
(i
|
||||||
|
@ -1120,8 +1140,8 @@ y
|
||||||
This is the classic Lisp/Scheme/Racket idiom sometimes called "let
|
This is the classic Lisp/Scheme/Racket idiom sometimes called "let
|
||||||
over lambda". @margin-note*{A
|
over lambda". @margin-note*{A
|
||||||
@hyperlink["http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html" "koan"]
|
@hyperlink["http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html" "koan"]
|
||||||
about closures and objects.} A closure hides @racket[y], which can't
|
about closures and objects.} A closure hides @racket[y], which can
|
||||||
be accessed directly, only via @racket[get-y].
|
only be accessed via @racket[get-y].
|
||||||
|
|
||||||
So why would we care about the splicing forms? They can be more
|
So why would we care about the splicing forms? They can be more
|
||||||
concise, especially when there are multiple body forms:
|
concise, especially when there are multiple body forms:
|
||||||
|
@ -1154,7 +1174,7 @@ The splicing variation is more convenient than the usual way:
|
||||||
When there are many body forms---and you are generating them in a
|
When there are many body forms---and you are generating them in a
|
||||||
macro---the splicing variations can be much easier.
|
macro---the splicing variations can be much easier.
|
||||||
|
|
||||||
|
@; ----------------------------------------------------------------------------
|
||||||
@; ----------------------------------------------------------------------------
|
@; ----------------------------------------------------------------------------
|
||||||
|
|
||||||
@section{References and Acknowledgments}
|
@section{References and Acknowledgments}
|
||||||
|
@ -1201,6 +1221,7 @@ projected between the lines. My point is, the Racket documentation is
|
||||||
very good. The @italic{Guide} provides helpful examples and
|
very good. The @italic{Guide} provides helpful examples and
|
||||||
tutorials. The @italic{Reference} is very clear and precise.
|
tutorials. The @italic{Reference} is very clear and precise.
|
||||||
|
|
||||||
|
@; ----------------------------------------------------------------------------
|
||||||
@; ----------------------------------------------------------------------------
|
@; ----------------------------------------------------------------------------
|
||||||
|
|
||||||
@section{Epilogue}
|
@section{Epilogue}
|
||||||
|
@ -1218,7 +1239,6 @@ later translated by D.T. Suzuki in his @italic{Essays in Zen
|
||||||
Buddhism}.}
|
Buddhism}.}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Translated into Racket:
|
Translated into Racket:
|
||||||
|
|
||||||
@racketblock[
|
@racketblock[
|
||||||
|
@ -1231,4 +1251,5 @@ Translated into Racket:
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(and (eq? 'mountains 'mountains)
|
(and (eq? 'mountains 'mountains)
|
||||||
(eq? 'rivers 'rivers))))
|
(eq? 'rivers 'rivers))))
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user