function calling itself using a generic self reference call?

2012-08-06 Thread Dave Sann
(this is not a question about recur) Suppose I have: (defn v1 ([x] (v1 x [])) ([x r] (conj r x))) and I decide to make v2 - for whatever reason - say I think I have a better implementation for part of it so I start with a copy and edit: (defn v2 ([x] (v1 x [])) ([x r] (conj r

Re: function calling itself using a generic self reference call?

2012-08-06 Thread Carlo Zancanaro
1. would it be, or is it possible to have something generic like this? You could use: (def v1 (fn self ([x] (self x [])) ([x r] (conj r x))) 2. What about anonymous fns? The same idea, just without the def. (fn call-self ([x] (call-self x 0)) ([x y] ...))) -- You received this

Re: function calling itself using a generic self reference call?

2012-08-06 Thread Baishampayan Ghose
You might want to look at the Y Combinator for this. Regards, BG On Mon, Aug 6, 2012 at 11:36 AM, Dave Sann daves...@gmail.com wrote: (this is not a question about recur) Suppose I have: (defn v1 ([x] (v1 x [])) ([x r] (conj r x))) and I decide to make v2 - for whatever reason -

Re: function calling itself using a generic self reference call?

2012-08-06 Thread Dave Sann
I didn't know you could name functions made with fn like that. nice. It would be a handy feature for defn to have maybe... On Monday, 6 August 2012 16:16:06 UTC+10, Carlo wrote: 1. would it be, or is it possible to have something generic like this? You could use: (def v1 (fn self

Re: function calling itself using a generic self reference call?

2012-08-06 Thread Dave Sann
It would also be a useful ability in multimethods... On Monday, 6 August 2012 16:33:15 UTC+10, Dave Sann wrote: I didn't know you could name functions made with fn like that. nice. It would be a handy feature for defn to have maybe... On Monday, 6 August 2012 16:16:06 UTC+10, Carlo wrote:

Re: function calling itself using a generic self reference call?

2012-08-06 Thread nicolas.o...@gmail.com
(defn recurse [f] (fn [ args] (apply f (recurse f) args))) (def fac (recurse (fn [self n] (if (zero? n) 1 (* n (self (dec n))) There is a performance cost. recursive can be specialised on different number of arguments, to reduce this cost.