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: [email protected]
[mailto:[email protected]] 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

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to