views:

194

answers:

1

Hello. I am attempting to build a rudimentary interpreter in Scheme, and I want to use an association list 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 the elements on the RHS of the table are stored as lists of symbols. Does anyone have any ideas as to how to remedy his. Thanks in advance.

+6  A: 

You probably want:

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

Also, you can use a macro to make it easier to specify:

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