>My problem is not finding recursive code. >My problem is finding tail recursive code.
One example is a version of the genetic algorithm in the 'limit limitation' thread ( http://www.jsoftware.com/pipermail/programming/2009-October/016522.html ): CHARS =: ' ABCDEFGHIJKLMNOPQRSTUVWXYZ' mutation =: CHARS&(] ,: [ {~ $...@] ?...@$#@[) fivePct =: 0.05 >: $ ?...@$ 0: randomize =: (fivePct f.)`(mutation f.)} score =: +/@:~:"1 copy100 =: 100 $ ,: NotMatch =: 1 - -: initial =: CHARS ([ {~ ?...@$&#~ ) [ next =: ((i. <./)@:score { ]) randomize@:copy100@:] recur=. ]`([ $: next)@.NotMatch evolve=. recur initial evolve 'METHINKS IT IS LIKE A WEASEL' METHINKS IT IS LIKE A WEASEL evolve 'HOWEVER SOMETIMES IT MAY NOT WORK PROPERLY FOR SENTENCES OF CERTAIN LENGTH' |stack error: randomize | evolve'HOWEVER SOMETIMES IT MAY NOT WORK PROPERLY FOR SENTENCES OF CERTAIN LENGTH' |[-18] >So, ok, let's just work with factorial. ________________________________ From: Raul Miller <[email protected]> To: Programming forum <[email protected]> Sent: Tue, December 22, 2009 7:54:18 PM Subject: Re: [Jprogramming] Recursive tail calls (WAS: Tacit exercise) On Tue, Dec 22, 2009 at 6:44 PM, Dan Bron <[email protected]> wrote: > If I were interested in specific examples of RTCs in J (as opposed to > supporting TCO as general, special code) I might think of tree applications, > where trees are represented by nested boxes. Of course, trees can be > adequately represented by (sparse) open arrays and handled as such, but at > least my first instinct in processing trees is boxes and $: . I would > call that the "natural approach", and it is the sort of thing I would like > to see optimized (so that I don't have to come up with insightful new > approaches). > > I might also search the Forum archives for instances of the phrase "stack > error", or $: more generally (which won't be too overwhelming, as $: > doesn't see much use in J, for the reasons previously discussed). My problem is not finding recursive code. My problem is finding tail recursive code. So, ok, let's just work with factorial. This is a recursive implementation of factorial which is not tail recursive: 1:`(* $:@<:)@.* 3 This is a recursive implementation of factorial which is tail recursive: 1 ([`(* $: <:@])@.(*...@])) 3 (Note, by the way, that the $: mechanism means that I can not incorporate the required left argument (1) into the definition of this verb.) Anyways, with just these two verbs as examples, the task of recognizing (and automatically rewriting) tail recursive verbs seems potentially challenging. -- Raul ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
