Igm*_*man 2 lisp scheme interpreter functional-programming
我试图在Scheme中构建一个基本的解释器,我想使用一个关联列表映射到算术函数.这是我到目前为止:
; A data type defining an abstract binary operation
(define binoptable
'(("+" . (+ x y)))
("-" . (- x y))
("*" . (* x y))
("/" . (/ x y)))
)
Run Code Online (Sandbox Code Playgroud)
问题是表的RHS上的元素存储为符号列表.有没有人对如何补救他有任何想法.提前致谢.
你可能想要:
(define binoptable
`(("+" . ,+)
("-" . ,-)
("*" . ,*)
("/" . ,/)))
Run Code Online (Sandbox Code Playgroud)
此外,您可以使用宏来更容易指定:
(define-syntax make-binops
(syntax-rules ()
[(make-binops op ...)
(list (cons (symbol->string 'op) op) ...)]))
(define binoptable (make-binops + - * /))
Run Code Online (Sandbox Code Playgroud)