【问题标题】:Function Table in Scheme using Association ListScheme中使用关联列表的函数表
【发布时间】:2009-09-29 06:01:26
【问题描述】:

我正在尝试在 Scheme 中构建一个基本的解释器,并且我想使用关联列表来映射到算术函数。这是我到目前为止所拥有的:

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

问题是表的 RHS 上的元素存储为符号列表。有没有人对如何补救他有任何想法。提前致谢。

【问题讨论】:

    标签: functional-programming lisp scheme interpreter


    【解决方案1】:

    你可能想要:

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

    此外,您可以使用宏来更轻松地指定:

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

    【讨论】:

      猜你喜欢
      • 2014-05-03
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多