简体   繁体   中英

Referencing unbounded function in Clojure REPL

In the Common Lisp REPL I can do that:

>(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)

If I try the same in Clojure the result is this:

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!

Why?

in clojure use declare to create forward references.

(declare sq)
(defn sos [x y] (+ (sq x) (sq y)))

This part of the one-pass compiler design decision.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM