macrospattern-matchingracketdefine-syntax

Racket macro for expanding code


I want to be able to write:

(nota E2 82)

instead of:

(define E2
  (network ()
           [sunet <= sine-wave 82]
           [out = (+ sunet)]))

I know I can do this using macros and tried to write this:

(define-syntax (nota stx)
  (syntax-case stx ()
    [(nota x) #'(network ()
                         [sunet <= sine-wave x]
                         [out = (+ sunet)])]))

But I get this error:

nota: bad syntax in: (nota E2 82)

Solution

  • The simplest solution would be

    (define-syntax-rule (nota x y)
      (define x
        (network ()
                 [sunet <= sine-wave y]
                 [out = (+ sunet)])))