On this page:
define-shorthand
define-id-shorthand
define-alias
define-aliases
with-aliases
8.12

3.1 Writing Simple Macros with mischief/shorthand🔗ℹ

 (require mischief/shorthand) package: mischief-dev

syntax

(define-shorthand (id . pattern) template)

(define-shorthand id [pattern template] ...)
Defines id as a macro that matches applications against each pattern and produces the corresponding template for the first match. Non-identifier macros with only a single pattern can use the simplified form that combines the pattern with the macro name. Each pattern is interpreted as in syntax-parse.

Examples:
> (define-shorthand (lam x e) (lambda {x} e))
> (define-shorthand def
    [(_ x:id e) (define x e)]
    [(_ (f x) e) (def f (lam x e))])
> (def (len seq) (for/fold {[n 0]} {[x seq]} (add1 n)))
> (len (list 1 2 3))

3

> (len (vector 1 2 3))

3

syntax

(define-id-shorthand name template)

Defines name as an identifier macro that expands by replacing name with template.

Examples:
> (define counter (box 0))
> (define-id-shorthand current (unbox counter))
> (define-id-shorthand increment!
    (set-box! counter (add1 current)))
> current

0

> increment!
> current

1

> increment!
> current

2

> (current 'bad 'application)

application: not a procedure;

 expected a procedure that can be applied to arguments

  given: 2

syntax

(define-alias id target-id)

syntax

(define-aliases [id target-id] ...)

Defines each id as an alias for the corresponding target-id using make-rename-transformer.

Examples:
> (define-alias value quote)
> (define-aliases [choose cond] [otherwise else])
> (define (sum xs)
    (choose
      [(empty? xs) (value 0)]
      [otherwise (+ (first xs) (sum (rest xs)))]))
> (sum (list 1 2 3 4))

10

syntax

(with-aliases {[id target-id] ...} body ...+)

Defines each id as a local alias for the corresponding target-id within the body of the expression.

Examples:
> (with-aliases {[cake pi]} (* cake 2))

6.283185307179586

> cake

cake: undefined;

 cannot reference an identifier before its definition

  in module: 'program