Template macros combine the flexibility of template meta-programming with the safety of Racket's hygienic macro sub-system.
Template variables are resolved before expansion by selectively rewriting the input text. The extra flexibility makes escaping to the expanding environment less necessary and more convenient.
#lang racket/base
(require racket/match template (for-syntax racket/base))
(define-for-syntax the-fibs
(make-immutable-hash
(for/fold ([fibs '([1 . 1] [0 . 0])])
([k (in-range 2 10)])
`([,k . ,(+ (cdar fibs) (cdadr fibs))] ,@fibs))))
(begin-template '#,(map cdr (sort (hash->list the-fibs) < #:key car)))
; '(0 1 1 2 3 5 8 13 21 34)
(begin-template
(define fib (match-lambda (for/template ([K (in-range 10)])
[K #,(hash-ref the-fibs K)]))))
(fib 8)
; 21
(fib 10)
; match-lambda: no matching clause for 10
Template macros are distributed in the
template package on the
official Racket package repository. It can be installed from DrRacket's
package manager, or with raco pkg
from the comand line.
raco pkg install template
To start using template macros, import the template
collection.
(require template)
See the official documentation for a detailed overview of template macros, along with a catalog of template constructors, combiners, and definers.
Pull requests of any size are welcome. For help creating one, or to propose major changes, please open an issue first to discuss what you would like to change.