You're viewing version 1.2.0 of letfn. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

letfn

clojure.core

  • (letfn fnspecs & body)
Takes a vector of function specs and a body, and generates a set of
bindings of functions to their names. All of the names are available
in all of the definitions of the functions, as well as the body.

fnspec ==> (fname [params*] exprs) or (fname ([params*] exprs)+)

1 Example top

  • user=> (letfn [(twice [x]
                     (* x 2))
                   (six-times [y]
                     (* (twice y) 3))]
             (println "Twice 15 =" (twice 15))
             (println "Six times 15 =" (six-times 15)))
    Twice 15 = 30
    Six times 15 = 90
    nil
    
    ;; Unable to resolve symbol: twice in this context
    user=> (twice 4)
    ; Evaluation aborted.
    
    ;; Unable to resolve symbol: six-times in this context
    user=> (six-times 100)
    ; Evaluation aborted.
    
Log in to add / edit an example.

See Also top

  • 0
    clojure.core/let

    Evaluates the exprs in a lexical context in which the symbols in th

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:5246 top

(defmacro letfn 
  "Takes a vector of function specs and a body, and generates a set of
  bindings of functions to their names. All of the names are available
  in all of the definitions of the functions, as well as the body.

  fnspec ==> (fname [params*] exprs) or (fname ([params*] exprs)+)"
  {:added "1.0"}
  [fnspecs & body] 
  `(letfn* ~(vec (interleave (map first fnspecs) 
                             (map #(cons `fn %) fnspecs)))
           ~@body))
Vars in clojure.core/letfn: cons defmacro first interleave map vec concat fn list seq

Comments top

1 comment(s) for letfn.

Using letfn allows you to create local functions that reference each other whereas (let myfunc #(...)]...) wouldn't because it executes its bindings serially.

Log in to add a comment.