我正在尝试将 SICP 的元循环评估器转换为 Clojure。在setup-environment
调用extend-environment
中无法编译,因为我收到错误“尝试调用未绑定的 fn”。以下是部分代码:
(... loads of methods for creating and managing environment list)
(def primitive-procedures
(list (list 'car first)
(list 'cdr rest)
(list 'cons conj) ;; TODO: reverse
(list 'null? nil?)
(list 'list list)
(list '+ +)
(list '- -)
(list '* *)
(list '/ /)
;; more primitives
))
(def primitive-procedure-names
#(map [first
primitive-procedures]))
(def primitive-procedure-objects
(fn [] (map (fn [p] (list 'primitive (second p)))
primitive-procedures)))
(def the-empty-environment '())
(defn extend-environment [vars vals base-env]
(if (= (count vars) (count vals))
(conj base-env (make-frame vars vals))
(if (< (count vars) (count vals))
(throw (Throwable. "Too many arguments supplied") vars vals)
(throw (Throwable. "Too few arguments supplied") vars vals))))
;; Added # in front here so it could be called (???)
(defn setup-environment []
#(let [initial-env
(extend-environment (primitive-procedure-names)
(primitive-procedure-objects)
the-empty-environment)] ;; <= that does not work
(define-variable! 'true true initial-env)
(define-variable! 'false false initial-env)
initial-env)))
;; Method for interacting with the evaluator:
(defn driver-loop []
(prompt-for-input input-prompt)
(let [input (read)]
(let [output (m-eval input the-global-environment)]
(announce-output output-prompt)
(user-print output)))
(driver-loop))
(...)
(def the-global-environment (setup-environment))
(driver-loop)
当我评估该extend-environment
方法时,我收到以下错误:
- 由 java.lang.IllegalStateException 引起
尝试调用未绑定的 fn:
#'scheme-evaluator/extend-environment
Var.java:43 clojure.lang.Var$Unbound/throwArity
AFn.java:40 clojure.lang.AFn/invoke
scheme- evaluator.clj: 277 方案评估器/eval7808
我认为我没有提供正确类型的参数,或者我没有创建正确类型的函数。我尝试了各种匿名方法的变体,并在括号中传递或不传递,但我无法编译它。
有谁知道这个错误的原因是什么,我该如何解决?