在 Clojure REPL 中引用无限函数
Referencing unbounded function in Clojure REPL
在 Common Lisp REPL 中我可以做到:
>(DEFUN SOS (x y) (+ (sq x) (sq y)))
SOS
>(sos 5 4)
Error in SOS [or a callee]: The function SQ is undefined.
Fast links are on: do (use-fast-links nil) for debugging
Broken at +. Type :H for Help.
1 (Abort) Return to top level.
dbl:>>1
Top level.
>(DEFUN sq (x) (* x x))
SQ
>(sos 5 4)
41
>(quit)
如果我在 Clojure 中尝试相同的结果是这样的:
user=> (defn sos [x y] (+ (sq x) (sq y)))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: sq in this context, compiling:(NO_SOURCE_PATH:1:20)
user=> (quit)
Bye for now!
为什么?
在 clojure 中使用 declare
创建前向引用。
(declare sq)
(defn sos [x y] (+ (sq x) (sq y)))
这部分由one-pass编译器设计决定。
在 Common Lisp REPL 中我可以做到:
>(DEFUN SOS (x y) (+ (sq x) (sq y)))
SOS
>(sos 5 4)
Error in SOS [or a callee]: The function SQ is undefined.
Fast links are on: do (use-fast-links nil) for debugging
Broken at +. Type :H for Help.
1 (Abort) Return to top level.
dbl:>>1
Top level.
>(DEFUN sq (x) (* x x))
SQ
>(sos 5 4)
41
>(quit)
如果我在 Clojure 中尝试相同的结果是这样的:
user=> (defn sos [x y] (+ (sq x) (sq y)))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: sq in this context, compiling:(NO_SOURCE_PATH:1:20)
user=> (quit)
Bye for now!
为什么?
在 clojure 中使用 declare
创建前向引用。
(declare sq)
(defn sos [x y] (+ (sq x) (sq y)))
这部分由one-pass编译器设计决定。