Functional programming 使用关联列表的方案中的函数表
我试图在Scheme中构建一个基本的解释器,我想使用关联列表映射到算术函数。这就是我到目前为止所做的:Functional programming 使用关联列表的方案中的函数表,functional-programming,lisp,scheme,interpreter,Functional Programming,Lisp,Scheme,Interpreter,我试图在Scheme中构建一个基本的解释器,我想使用关联列表映射到算术函数。这就是我到目前为止所做的: ; A data type defining an abstract binary operation (define binoptable '(("+" . (+ x y))) ("-" . (- x y)) ("*" . (* x y)) ("/" . (/ x y))) ) 问题是表的右侧的元素存储为符号列表。有没有人对如何补救他的错误有什么想法。提前感谢。
; A data type defining an abstract binary operation
(define binoptable
'(("+" . (+ x y)))
("-" . (- x y))
("*" . (* x y))
("/" . (/ x y)))
)
问题是表的右侧的元素存储为符号列表。有没有人对如何补救他的错误有什么想法。提前感谢。您可能需要:
(define binoptable
`(("+" . ,+)
("-" . ,-)
("*" . ,*)
("/" . ,/)))
此外,还可以使用宏来更容易地指定:
(define-syntax make-binops
(syntax-rules ()
[(make-binops op ...)
(list (cons (symbol->string 'op) op) ...)]))
(define binoptable (make-binops + - * /))