Newsgroups: comp.lang.apl
Path: watmath!watserv2.uwaterloo.ca!torn!utcsri!rpi!usc!wupost!uunet!uunet.ca!geac!itcyyz!yrloc!hui
From: hui@yrloc.ipsa.reuter.COM (Roger Hui)
Subject: Re: Question about recursion in J
Message-ID: <1992Aug12.053055.23246@yrloc.ipsa.reuter.COM>
Organization: Iverson Software Inc.
References: <1992Aug7.215222.13596@csus.edu>
Date: Wed, 12 Aug 92 05:30:55 GMT
Lines: 87

Emmett McLean writes:
 
> I have been trying to get a feel for recursion in J.
> There are plenty of examples of recursion in the tutorial
> but I couldn't find one where there a call to the recursive
> funtion had variable arguments like those which might be
> found in other languages. (Perhaps I just don't know it
> when I see it.)  What I mean by "other languages" is made
> clear by this exercise , which I could only finish half
> way : translate the lisp program count-up-recursively into J.
 
> (defun count-up-recursively (cnt n)
> (cond ((> cnt n) nil)
>       (t (cons cnt
>       (count-up-recursively (+ cnt 1) n )))))
 
The LISP function has two named arguments and translates naturally
into a dyadic verb in J.  If an explicit verb is used, the arguments
would be named x. and y. (corresponding to cnt and n); if a tacit verb
is used, the arguments would be tacit (unnamed).  Thus:
 
   t=.0 0$''
   t=.t, '$.=.1+x.>y.'       NB. (> cnt n)
   t=.t, 'x.,(>:x.) cure y.' NB. (cons cnt (count-up-recursively (+ cnt 1) n))
   t=.t, '$0'                NB. nil
   cure =. '' : t            NB. (defun count-up-recursively (cnt n)
 
   curt =. ([ , >:@[ curt ])`(i.@0:) @. >
 
   3 cure 7          3 curt 7
3 4 5 6 7         3 4 5 6 7
 
As illustrated, recursion can be effected by appropriate naming.
For tacit verbs there is also the possibility of anonymous recursion,
using $:, which denotes the verb being defined.  Thus:
 
   _3 ([ , >:@[ $: ])`(i.@0:)@.> 2
_3 _2 _1 0 1 2
 
Since the computation can be stated non-recursively as  [ + i.@(0&>.)@-.@-
it is not a very good example of recursion.  As more of a challenge, I offer
the following:
 
A partition of positive integer n is a list v of positive integers such
that n=+/v.  "part" and "pare" compute the sorted table of sorted size-m
partitions of n.
 
   start =. +/@{. >:@i.@<.@%&>: {:@$
   mask  =. start <:/ {."1 <. -.@(-/)@(_2&{.)"1
   pfx   =. +/"1 # >:@i.@#
   ind   =. , # */@$ $ i.@{:@$
   decr  =. (>:@(-/)@(_1 0&{) _1} ])"1
   form  =. pfx@[ decr@,. ind@[ { ]
   recur =. (mask form ])@(part&<:)
 
   test  =. 1&<@[ *. <
   basis =. (0&<@] , [) $ (1&=@[ 1&>.@* ])
   part  =. basis`recur@.test
 
   1 part 6        2 part 7        3 part 8         4 part 9
6               1 6             1 1 6            1 1 1 6
		2 5             1 2 5            1 1 2 5
                3 4             1 3 4            1 1 3 4
                                2 2 4            1 2 2 4
                                2 3 3            1 2 3 3
                                                 2 2 2 3
   t=.0 0$0
   t=.t, ' $.=.>((1<x.)*.x.<y.){1;L20'
   t=.t, ' ((0<y.),x.)$1>.y.*1=x.'
   t=.t, 'L20)'
   t=.t, ' k=.>:i.<.y.%x.'
   t=.t, ' t=.x. pare&<: y.'
   t=.t, ' b=.k<:/({."1 t)<.-.-/@(_2&{.)"1 t'
   t=.t, ' z=.((+/"1 b)#k),"_1 ((#t)|(,b)#i.*/$b){t'
   t=.t, ' (0 _1}.z),.>:@(-/)@(_1 0&{)"1 z'
   pare =. '' : t
 
   4 (pare -: part) 9
1
 
The RI and QR examples posted last week in article <1992Aug6.033157.6833@
yrloc.ipsa.reuter.com> also illustrate the interplay between recursion,
tacit and explicit definition, and arrays.

------------------------------------
Roger Hui, Iverson Software Inc., 33 Major Street, Toronto, Ontario  M5S 2K9
Phone: (416) 925 6096;  Fax: (416) 488 7559
