Search code examples
clojure

define recursive function in let form


Is it possible in Clojure to define a recursive function inside a let form or should a letfn be used instead? For instance, can I do the below using let?

(defn blowStackExample []
  (letfn [(blowStack []
            (blowStack))]
    (blowStackExample))) 

Solution

  • The way you can do it is by naming the anonymous function inside the fn form:

    (defn blow-up-stack-example []
      (let [blow-up-stack (fn a [] (a))]
        (blow-up-stack)))
        
    (blow-up-stack-example)