[Jprogramming] tail recursion [bug?]
Dan Bron
j at bron.us
Mon Jan 14 19:33:23 UTC 2013
I haven't looked at this in detail, but I would be suspicious of (coerase
m)](r__m) . Try breaking that up across multiple lines.
-Dan
-----Original Message-----
From: programming-bounces at forums.jsoftware.com
[mailto:programming-bounces at forums.jsoftware.com] On Behalf Of Raul Miller
Sent: Saturday, January 12, 2013 8:31 PM
To: Programming forum
Subject: [Jprogramming] tail recursion [bug?]
I wrote an implementation of tail recursion in J
It is designed to only work on verbs with the following constraints:
1. The verb is either non-recursive or tail recursive 2, The verb is tacit
3. The verb does not reference reserved names (m, n, u, v, x, y)
A tail recursive verb is a singly recursive verb where the result of any
recursive case will be delivered by the non-recursive case (without any
further processing). And note that this is a constraint on rank, also.
Argument pattern is:
'verbname' tailr (verbdef)
where verbname is the name used in the tacit verbdef.
Unfortunately, when I try it out on a recursive case, it crashes J.
Here's the definition:
tailr=: 2 :0
L1=. 'n=.;m=. cocreate''''',LF
L2=. 'y__m=: y',LF
L4=. m,'=.3
:(''y_'',n,''_=:y'',LF,''n_'',n,''_=:1'',LF,'':'',LF,''x_'',n,''_=:x'',LF,''
y_'',n,''_=:y'',LF,''n_'',n,''_=:1'',LF)',LF
L5=. 'label_start.',LF
L6=. 'y=. y__m',LF
L8=. 'n__m=:0',LF
L9a=.'r__m=:'
L9b=.'(',(5!:5<'v'),') y',LF
L10=. 'if. n__m do. goto_start. end.',LF
L11=. '(coerase m)](r__m)',LF
P1=. L1,L2
P2=. L4,L5,L6
P3=. L8,L9a
P4=. L9b,L10,L11
(m)=: 3 :(P1,P2,P3,P4,':',LF,P1,'x__m=: x',LF,P2,'x=. x__m',LF,P3,'x',P4)
m
)
Here's an example, based on
http://www.jsoftware.com/help/dictionary/intro22.htm
'factorial' tailr (1:`(]*factorial@<:) @. *)
This works for me:
factorial 0
This crashes for me:
factorial 1
Oh well...
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list