Function table in schema using association list

I am trying to create a rudimentary interpreter in Scheme, and I want to use a list of associations to map to arithmetic functions. This is what I have so far:

; A data type defining an abstract binary operation
(define binoptable
  '(("+" . (+ x y)))
    ("-" . (- x y))
    ("*" . (* x y))
    ("/" . (/ x y)))
)

      

The problem is that the items in RHS tables are stored as character lists. Does anyone have any ideas on how to fix it. Thanks in advance.

+2


source to share


1 answer


You probably want:

(define binoptable
  `(("+" . ,+)
    ("-" . ,-)
    ("*" . ,*)
    ("/" . ,/)))

      



Alternatively, you can use a macro to make it easier to define:

(define-syntax make-binops
  (syntax-rules ()
    [(make-binops op ...)
     (list (cons (symbol->string 'op) op) ...)]))
(define binoptable (make-binops + - * /))

      

+6


source







All Articles