Newsgroups: comp.lang.apl
Path: watmath!watserv1!utgpu!news-server.csri.toronto.edu!torsqnt!tmsoft!itcyyz!yrloc!hui
From: hui@yrloc.ipsa.reuter.COM (Roger Hui)
Subject: Re: Library of J "functions"?   (was Re: Meaning of Curry in J)
Message-ID: <1992Feb16.052203.7834@yrloc.ipsa.reuter.COM>
Organization: Iverson Software Inc.
References: <920204044743_72740.1026_EHE37-1@CompuServe.COM> <ROCKWELL.92Feb4081353@socrates.umd.edu> <1992Feb8.045229.23356@yrloc.ipsa.reuter.COM> <1992Feb11.224117.15233@apollo.hp.com>
Date: Sun, 16 Feb 92 05:22:03 GMT

Mark Keil writes:
 
> Roger, Do you have any more of these? In fact to you have a library(s)
> of useful J stuff like above? It might be a help if a base library of
> J "functions" was available.
 
Since you asked :-)
 
First, since this sequence of msgs began with bonding an argument to
a primitive ("Currying"), herewith a few examples of bonding a noun
to a verb, to derive a verb.  They are to illustrate bonding and may or
may not be useful.
 
Negate =. 0&-    Incr =. 1&+    Sqrt =. ^&0.5        Base2Log  =. 2&^.
Negate =. _1&*   Decr =. -&1    CubeRoot =. ^&(%3)   Base10Log =. 10&^.
Negate =. *&_1   Decr =. _1&+   CubeRoot =. 3&%:     Base10Antilog =. 10&^
 
Sin =. 1&o.   Arcsin =. _1&o.   Sinh =. 5&o.    Arcsinh =. _5&o.
Cos =. 2&o.   Arccos =. _2&o.   Cosh =. 6&o.    Arccosh =. _6&o.
Tan =. 3&o.   Arctan =. _3&o.   Tanh =. 7&o.    Arctanh =. _7&o.
 
RemoveBlanks =. -.&' '          FirstDiff =. 2&(-/\)
AsciiCode    =. a.&i.           QuarterMovAvg =. 4&(mean\)
Diag =. (<0 1)&|:               QuarterTotal  =. _4&(+/\)
 
Next, an adverb which generalizes the "expand" derived function in APL.
 
   exp =. (@]) ((*+/\)@[`{`) (`(,`])) \
 
   x =. 0 1 0 0 1 1
   x 0: exp 5 6 7             x 57&[ exp i.3 4
0 5 0 0 6 7                57 57 57 57
                            0  1  2  3
                           57 57 57 57
                           57 57 57 57
                            4  5  6  7
                            8  9 10 11
 
   x '.'&[exp 'abc'          x {:exp >;:'foo upon thee'
.a..bc                    thee
                          foo
                          thee
                          thee
                          upon
                          thee
 
In x v exp y, x is a boolean list with ones indicating items to be
selected in order from y, and zeros indicating fill items; verb v
applies to y to obtain the fill item.
 
Finally, for a little J divertissement, something completely different.
Ackerman's function is defined on the non-negative integers as follows:
 
   ack =. c1`c1`c2`c3 @. (#.@(,&*))
   c1  =. >:@]
   c2  =. <:@[ ack 1:
   c3  =. <:@[ ack [ ack <:@]
 
   2 ack 3
9
   3 ack 2
29
 
Lemma:  If x ack y is f&.(3&+) y, then (x+1) ack y is f^:(1+y)&.(3&+) 1.
Using the lemma (or otherwise), it can be shown that:
 
0&ack  -:   >:&.(3&+)             NB.  >:
1&ack  -:  2&+&.(3&+)             NB.  2&+
2&ack  -:  2&*&.(3&+)             NB.  3&+@(2&*)
3&ack  -:  2&^&.(3&+)
4&ack  -:  ^/@(#&2)&.(3&+)
5&ack  -:  '^/@(#&2)^:(1+y.)&.(3&+) 1' : ''

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