Racket Macro for Code Extension

I want to write:

(nota E2 82)

instead:

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

I know that I can do this with 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)
+4
source share
2 answers

The simplest solution would be

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

Well, that’s just awful. You really don't need to write this macro; There should be a form that supplies fixed network inputs.

In fact, there is. But ... it is not documented, and it is poorly named. It is currently called fixed-inputs, but I will rename it as I network-constdocument it.

Thanks for the tip about this!

John

+3
source

Source: https://habr.com/ru/post/1625174/


All Articles