How do I create a recursive anonymous function in Clojure which is not tail recursive?
The following clearly doesn't work, as recur
is only for tail recursive functions. I'm also reluctant to drag in a y-combinator..
((fn [n] (if (= 1 n) 1 (* n (recur (dec n))))) 5)
Anonymous recursion is primarily of use in allowing recursion for anonymous functions, particularly when they form closures or are used as callbacks, to avoid having to bind the name of the function. Anonymous recursion primarily consists of calling "the current function", which results in direct recursion.
Non-Tail / Head Recursion A function is called the non-tail or head recursive if a function makes a recursive call itself, the recursive call will be the first statement in the function. It means there should be no statement or operation is called before the recursive calls.
In tail recursion, there is no other operation to perform after executing the recursive function itself; the function can directly return the result of the recursive call. In non-tail recursion, some operations need to be performed using the returned value of the recursive call.
No. Go for readability. Many computations are better expressed as recursive (tail or otherwise) functions. The only other reason to avoid them would be if your compiler does not do tail call optimizations and you expect you might blow the call stack.
Functions can be given a name to refer to themselves by specifying it between fn
and the arglist:
user> ((fn ! [n] (if (= 1 n) 1 (* n (! (dec n))))) 5) 120
Here's a way that keeps it anonymous, mostly:
(((fn [!] (fn [n] (if (= 1 n) 1 (* n ((! !) (dec n)))))) (fn [!] (fn [n] (if (= 1 n) 1 (* n ((! !) (dec n))))))) 5)
It's not quite the Y combinator, but it does contain the same bit of self-application that allows Y to do its thing. By having a copy of the entire function in scope as !
whenever you need it, you can always make another copy.
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