[racket] Define several procedures in a macro

From: William James (w_a_x_man at yahoo.com)
Date: Thu Jan 22 07:27:16 EST 2015

The old-fashioned way:

(require compatibility/defmacro)

(defmacro define-instruction (name&args body)
  (let ((name (car name&args)) (args (cdr name&args)) (body (cdr body)))
    `(define (,(string->symbol (format "~a-simulation" name)) , at args)
      , at body)))

(define-instruction (hello a b c)
  (sim (+ a b c)))

(hello-simulation 1 2 3)
;; > 6


--------------------------------------------
On Wed, 1/21/15, Peter Samarin <petrsamarin at gmail.com> wrote:

 Subject: [racket] Define several procedures in a macro
 To: users at racket-lang.org
 Date: Wednesday, January 21, 2015, 3:21 PM
 
 
 
 Here is the construction of the simulator procedure:
 
 (define-syntax define-instruction
    (syntax-rules (sim)
      [(_ (name . args) (sim body-r
 ...))
       (begin
         (define name-string
 (symbol->string 'name))
         (define simulator-name
           (string->symbol
        
    (string-append
             name-string
 "-simulation")))
         (eval `(define (,simulator-name
 . args)
              
    body-r ...)))]))
 
 (define-instruction (hello a b c)
    (sim
     (+ a b c)))
 
 (hello simulation 1 2 3)
 ;; > 6
 
 


Posted on the users mailing list.