Desired Thinking in Clojure

In the circuit I used to do something like this

(define (f x) (g x))
(define (g x) (+ x 42))
(g 0)

That is, I was used to define functions in terms of other instantly unlimited functions. Why is this not possible in Clojure? For example, in Clojure REPL, the following is not valid

(defn f [x] (g x))
(defn g [x] (+ x 42))
(g 0)
+4
source share
1 answer

The thing is that each line is compiled into repl, so the function is gnot executed when compiling f. You must add (declare g)before (defn f...so that the compiler knows about this function:

user> (declare g)
#'user/g

user> (defn f [x] (g x))
#'user/f

user> (defn g [x] (+ x 42))
#'user/g

user> (g 0)
42
+11
source

All Articles