purpose of defn in Clojure -
i'm little puzzled role of defn. if fn produced anonymous functions, understand need construct combines functionality of def , fn, fn can create named functions. @ least in repl, don't see how usage differs defn.
when provide name symbol fn
, it's only bound within function definition function object itself. allow anonymous functions call (clojure - special forms).
so create function fn
bound global accessible name, have use
(def somename (fn ...body...
and defn
shortcut this.
(defn somename ...body...
in response comment, fresh repl:
give me clojure: > (fn foo [] (+ 1 3)) #<sandbox31764$eval31779$foo__31780 sandbox31764$eval31779$foo__31780@12eae8eb> > (foo) java.lang.runtimeexception: unable resolve symbol: foo in context > (defn foo [] (+ 1 3)) #'sandbox31764/foo > (foo) 4 >
as can see, can't call foo
function created fn
, because it's not bound var.
Comments
Post a Comment