In Scheme I was 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 momentaneously unbounded functions. Why isn't this possible in Clojure? For example on a Clojure REPL the following isn't valid
(defn f [x] (g x))
(defn g [x] (+ x 42))
(g 0)
the thing is every line is being compiled in repl, so there is no g
function when f
is being compiled. You should add (declare g)
before (defn f...
so the compiler would be aware of 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With