Hacker News new | past | comments | ask | show | jobs | submit login

Oddly enough I never used this, but maybe named fns might help? (http://clojure.org/special_forms#Special%20Forms--%28fn%20na...)

    (fn my-name []
      (throw (Exception. "boom")))
(Sorry, I don't know Clojure internals well enough to have great answers to your explicit questions.)

[Edited: thanks to jerf for pointing out the inconsistency in a term I used.]




Named... anonymous... functions? Wuzzah fuzzah?


They allow you to use recursion without the need for a Y-combinator.


Yeah, that's a terminology issue. They mean lexically scoped named functions. Versus the default in Clojure of global or namespace scope.


Just like in Javascript.


Named anonymous functions are there to allow you to call the function recursively while inside it.

i.e.

    (= ((fn factorial [x] 
          (if (<= x 1) 
              1 
              (* x (factorial (dec x))))) 5)
     120)
Note, this isn't run under TCO (see loop/recur).


Also, in your example, the recursive call is not in tail position. You would first need to rewrite it to use an acumulator parameter.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: