Meaning of Curry in J
Author Message Meaning of Curry in J

I do not understand what is meant by "curry".  In the APL issue of the IBM
Systems Journal (Vol 30, No 4, 1991) there is a paper K. E. Iverson called "A
personal view of APL".  The last example in the paper is:

table=. / ([`by`]`over`) \
2 3 4 *table 1 2 3 4 5
+-+------------+
| |1 2  3  4  5|
|-+------------|
|2|2 4  6  8 10|
|3|3 6  9 12 15|
|4|4 8 12 16 20|
+-+------------+

(In the paper there is typography error making the second and fourth accent
marks look like quote marks.)

In the notation of "ISI Dictionary of J", section II(F)(b&c),
the expression

Clearly, for the correct result to obtain, the following must be true:

But what happens to make it so?  Can anyone explain curry in the context of
this example, perhaps by showing how the stack is processed?

Bob Hendricks

Sat, 23 Jul 1994 12:47:44 GMT  Meaning of Curry in J
Bob Hendricks:
In the APL issue of the IBM Systems Journal (Vol 30, No 4, 1991)
there is a paper K. E.  Iverson called "A personal view of APL".
The last example in the paper is:

table=. / ([`by`]`over`) \

presumably, with over and by defined as:

...

In the notation of "ISI Dictionary of J", section II(F)(b&c), the
expression

Clearly, for the correct result to obtain, the following must be true:

Um... that is not quite what happens.  But it is similar.  Actually,

Or, for this example:
2 3 4 * table 1 2 3 4 5
[A]              2 3 4 (  */  ([`by`]`over`) \)  1 2 3 4 5
[B]              2 3 4 (  [`by`]`over`(*/) \)    1 2 3 4 5
[C]              2 3 4 (  [ by (] over */))      1 2 3 4 5
[D]              2 3 4 by (1 2 3 4 5 over (2 3 4 */ 1 2 3 4 5))

at which point ([D]) the intermediates include a 3 by 5 array, and it
gets a little more akward to put the whole thing on a single line.
But I think you can take it from there.

--

The U.S. Federal government went another billion dollars into debt today.

Sat, 23 Jul 1994 21:13:53 GMT  Meaning of Curry in J

Quote:
Bob Hendricks writes:
> I do not understand what is meant by "curry".  In the APL issue of the IBM
> Systems Journal (Vol 30, No 4, 1991) there is a paper K. E. Iverson called "A
> personal view of APL".  The last example in the paper is:

>    table=. / ([`by`]`over`) \
>    2 3 4 *table 1 2 3 4 5
> +-+------------+
> | |1 2  3  4  5|
> |-+------------|
> |2|2 4  6  8 10|
> |3|3 6  9 12 15|
> |4|4 8 12 16 20|
> +-+------------+

> (In the paper there is typography error making the second and fourth accent
> marks look like quote marks.)
>    ...
> But what happens to make it so?  Can anyone explain curry in the context of
> this example, perhaps by showing how the stack is processed?

There are indeed typographical errors in "table" as it appeared in the
Systems Journal.  The text was recast with insufficient care _after_ a
final reading by Iverson.  (The version above is correct -- all grave
accents, no quotes.)

Raul Rockwell's reduction of "table" is useful and perhaps even easier
to understand, but the parser does something else.

Recall that a sentence to be parsed is placed on a queue, and as parsing
proceeds from right to left words are moved from the queue onto a stack.
An eligible portion of the stack is executed and replaced by the result.
Eligibility for execution is define by the 14-by-4 table on page 5
of the dictionary, and is completely determined by the _classes_
(noun, verb, adverb, etc.) of the first four words on the stack.

Assuming that "over" and "by" are previously defined as verbs,
then the sentence   table=./([`by`]`over`)\   is parsed as follows.
Each line represents a step in the parse.  Successive columns show
the index of the line, the queue, the stack, the applicable rule
and action, and comments.  ("M" is the left marker.)

Rule/

0  M table=./([`by`]`over`)\
1  M table=./([`by`]`over`)       \   13 Move
2  M table=./([`by`]`over`       )\   13 Move
3  M table=./([`by`]`over       `)\   13 Move
4  M table=./([`by`]`       over`)\   13 Move
5  M table=./([`by`]       `over`)\   13 Move
6  M table=./([`by`       ]`over`)\   13 Move
7  M table=./([`by       `]`over`)\   13 Move
8  M table=./([`       by`]`over`)\   13 Move
9  M table=./([       `by`]`over`)\   13 Move
10  M table=./(       [`by`]`over`)\   13 Move
11  M table=./       ([`by`]`over`)\   13 Move
12  M table=./         (n0`]`over`)\    4 Conj      n0=.[`by
13  M table=./           (n1`over`)\    4 Conj      n1=.n0`]
14  M table=./                (n2`)\    4 Conj      n2=.n1`over
15  M table=./                 (a0)\   10 Curry     a0=.n2`
16  M table=./                   a0\   12 Punct
17  M table=.                   /a0\   13 Move
18  M table                   =./a0\   13 Move
19  M table                     =.a1    7 Forkc     a1=./a0\
20  M                      table=.a1   13 Move
21  M                             a1   11 Is        table=.a1
22                              M a1   13 Move

The "Curry" action on line 15 applies because the first four words
of the stack (on the previous line) are "( n2 ` )", satisfying
the pattern for the rule.  The phrase n2` -- juxtaposing a conjunction
with an argument -- derives an adverb according to section II F
subsection b on page 5 of the dictionary.  This form of "Curry" has
many other useful instances.  For example:

Inverse   =. ^:_1      OnAtoms =. "0         Files    =. 1!:
Limit     =. ^:_       OnList  =. "1         WS       =. 2!:

The above shows how "table" is _defined_; the following is how "table"
is _applied_.  Note that the definition of "table" is not re-parsed
(i.e. it is compiled).

Rule/

0  M 3 4 *table 4 5 6
1  M 3 4 *table               4 5 6   13 Move
2  M 3 4 *              table 4 5 6   13 Move
3  M 3 4               *table 4 5 6   13 Move
4  M                3 4*table 4 5 6   13 Move
5  M                   3 4 v0 4 5 6    3 Adverb   v0=.*table
6                    M 3 4 v0 4 5 6   13 Move
7                              M n0    2 Dyad     n0 =. 3 4 v0 4 5 6

Finally, the verbs "over" and "by" used in "table" are defined as follows.
They have been revised and polished many times; your version may be
slightly different.

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

Wed, 27 Jul 1994 12:52:29 GMT  Meaning of Curry in J

<<Lots of stuff removed>>

Quote:
> This form of "Curry" has many other useful instances.  For example:

> Inverse   =. ^:_1      OnAtoms =. "0         Files    =. 1!:
> Limit     =. ^:_       OnList  =. "1         WS       =. 2!:

<<Lots of stuff removed>>

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.

Does anybody else have a library of useful J funtions
--
Mark Keil               HP/Apollo Computer,  Chelmsford MA 01824

Sun, 31 Jul 1994 06:41:17 GMT  Meaning of Curry in J

...
|> Does anybody else have a library of useful J funtions

I'm sure anyone who writes in J does. Useful to me may not be useful
to you though. Do you need to generate graphs in Xterms, or read VAX
ASCII F*****n numeric output?

--
Sam Sirlin

Mon, 01 Aug 1994 01:37:04 GMT  Meaning of Curry in J

Quote:

> ...
> |> Does anybody else have a library of useful J funtions

> I'm sure anyone who writes in J does. Useful to me may not be useful
> to you though. Do you need to generate graphs in Xterms, or read VAX
> ASCII F*****n numeric output?

Actually I have found it interesting to look a your previous draw.js script!
And I would be interested in seeing the VAX ASCII F*****n stuff.

Seeing other peoples stuff helps me learn idioms and exposes me to
interesting styles, and may save me a bunch of time not reinventing the wheel.

And for those interested, below are my aliases for external conjunctions (so far)
It's not complete yet, but it's a start.

Load from profile.js with:   0!:3 <'ext.js'

-Mark

ext.js
-------  cut here   ---------------------------------
NB.                 External conjunctions alaises
Host     =. 0!:0
Spawn    =. 0!:1
Script   =. 0!:2
SScript  =. 0!:3

Dir      =. 1!:0
Fwrite   =. 1!:2
Fappend  =. 1!:3
Fsize    =. 1!:4
IFwrite  =. 1!:12
Ferase   =. 1!:55

Nclass   =. 2!:0
Nlist    =. 2!:1

Stype    =. 3!:0
Inrep    =. 3!:1
CnvfrInv =. 3!:2

Nl       =. 4!:1
Vars     =. Nl 2
Fns      =. Nl 3
Nouns    =. Nl 2
Verbs    =. Nl 3
Conjs    =. Nl 5

Fix      =. 5!:0

TS       =. 6!:0 ''
Tsince   =. 6!:1 ''
SectoExe =. 6!:2
Delay    =. 6!:3

Spcnow   =. 7!:0 ''
Spctot   =. 7!:1 ''
Spcexe   =. 7!:2

Qcga     =. 8!:0
Qscrn    =. 8!:4

Qprompt  =. 9!:4
Sprompt  =. 9!:5
QBxchar  =. 9!:6
SBxchar  =. 9!:7
QJerr    =. 9!:8
SJerr    =. 9!:9

QRdecomp =. 128!:0
InvsuTri =. 128!:1
--
Mark Keil               HP/Apollo Computer,  Chelmsford MA 01824

Mon, 01 Aug 1994 05:16:00 GMT  Meaning of Curry in J
Mark Keil:
Load from profile.js with:   0!:3 <'ext.js'
..
Nl       =. 4!:1
Vars     =. Nl 2
Fns      =. Nl 3

Um.. a note of caution:  Nl 3  builds a list of verb names as of the
moment you execute it.  The pronoun 'Fns' is going to just be a list
of names, and isn't going to record that later on you'd defined Delay
to be 6!:3.   The same goes 'Vars' and all these:

Nouns    =. Nl 2
Verbs    =. Nl 3
Conjs    =. Nl 5

TS       =. 6!:0 ''
Tsince   =. 6!:1 ''

Spcnow   =. 7!:0 ''
Spctot   =. 7!:1 ''

When 6!:0 gets its argument (which it promptly ignores), it goes and
finds out what time it is.  If you want to get a similar behavior from
your names, I'd define them as functions, as follows:

Nouns=. Vars =.  2&[ Nl
Verbs=. Fns  =.  3&[ Nl
Conjs        =.  5&[ Nl
TS           =.  6!:0
Tsince       =.  6!:1
Spcnow       =.  7!:0
Spctot       =.  7!:1

Then you can do (Verbs ''), which should be simple enough to remember,
but will still get the job done.

--

The U.S. government went another thousand million dollars into debt today.

Mon, 01 Aug 1994 10:38:40 GMT  Meaning of Curry in J

|>   Actually I have found it interesting to look a your previous draw.js script!
|>   And I would be interested in seeing the VAX ASCII F*****n stuff.
|>
|>   Seeing other peoples stuff helps me learn idioms and exposes me to
|>   interesting styles, and may save me a bunch of time not reinventing the wheel.

Well I don't claim this stuff is particularly efficient, but since you

NB. for scripts
catb =. 'y.' : ' 1 |. y. ; x. '
com=. ' ''NB. '',y. ': ' 1 |. (com y.); x. '

NB. fix numerical data from other sources saved as text
NB. we could get something like  -.03D+05 or 23.
a=. com   'z=. fix y'
a=. a com '  fix text y (fortran output) for execution'
a=. a com '   first drop off initial comments, if present before /'
a=. a catb 'i=. y.  i. ''/'''
a=. a catb '\$. =. ,> (i = \$y. ){ \$.;next'
a=. a catb'y. =. (1 + i) }. y. '
a=. a catb'next) y. =. (''D'';''e'')replace y. '
a=. a catb'y. =. (''E'';''e'')replace y. '
a=. a catb'y. =. (''-'';''_'')replace y. '
a=. a catb'y. =. ((10{a.);'' '')replace y. '
a=. a catb'y. =. (''. '';''.0'')replace y. '
a=. a catb'y. =. ('' .'';'' 0.'')replace y. '
a=. a catb'y. =. (''+'';''0'')replace y. '
a=. a catb ' ". y.  '
fix=. a : ''

NB. the real work
a=. com    'r=. (a;b) replace text'
a=. a com  '  replace (vectors) a by b in vector text'
a=. a catb '(''a'';''b'')=. x.'
a=. a com  '  find locations of a in text'
a=. a catb '\$.=. ,>(0= +/i=. a E. y.){ \$.;end'
a=. a catb 'in=. find i'
a=. a catb 'na=.#a'
a=. a catb 'nb=.#b'
a=. a catb '\$.=. ,>(na<nb){ \$.;exp'
a=. a catb '\$.=. ,>(na>nb){ \$.;comp'
a=. a com  '  same size'
a=. a catb 'y.=. b (in +/ i.nb )} y.'
a=. a catb '\$.=. end'
a=. a catb 'comp) y.=. b (in +/ i.nb )} y.'
a=. a catb 'y.=. ( 0 (in +/ nb+i.na-nb)} (#y.)\$1 )#y.'
a=. a catb '\$.=. end'
a=. a catb 'exp) in=. in + (nb-na)*/i. \$in'
a=. a catb 'y.=.(,1+i*/nb-na) #y.'
a=. a catb 'y.=. b (in +/ i.nb )} y.'
a=. a catb 'end) y.'
replace=. '': a

NB. simple version now that E. is availble
NB.  only works for character vectors
find=. '(]#(i.&\$)) y.' : 'x. E. y.'

|>   And for those interested, below are my aliases for external conjunctions (so far)
...
|> Nl       =. 4!:1
|> Vars     =. Nl 2
|> Fns      =. Nl 3
...

Early on I found it annoying that these didn't respect window width,
and quickly become unreadable with many functions, so I wrote a
(pretty stupid) version that works for me:

a=. com ' r=. boxp b'
a=. a com '   print boxed vector'
a=. a catb 'width=. 10'
a=. a catb 'w=. width <. n=. \$y.'
a=. a catb 'm=. >. n%w'
a=. a catb 'r=. (m,w)\$ (m*w){. y.'
boxp=. a : ''

qnl=. 'boxp 4!:1 (y.)' : 'boxp x. 4!:1 (y.)'

for example the following becomes readable:

qnl 3
+---+----+------+---------+-------+-----+-----+-----+----+---+
|Nl |boxp|catb  |com      |comold |exit |input|print|pwd |qnc|
+---+----+------+---------+-------+-----+-----+-----+----+---+
+---+----+------+---------+-------+-----+-----+-----+----+---+
|z  |    |      |         |       |     |     |     |    |   |
+---+----+------+---------+-------+-----+-----+-----+----+---+

--
Sam Sirlin

Mon, 01 Aug 1994 10:36:45 GMT  Meaning of Curry in J
Sam Sirlin:
NB. fix numerical data from other sources saved as text
NB. we could get something like  -.03D+05 or 23.
a=. com   'z=. fix y'
a=. a com '  fix text y (Fortran output) for execution'
a=. a com '   first drop off initial comments, if present before /'

Hmm.. mind if I try?

NB.  use modulo to avoid dropping everything if no comment symbol present
dc =.  drop_comment =.  }.~     #  |&>:  i.&'/'

NB.  rely on i. returning first hit to do character substitution
lf =.  linefeed     =. 10{a.

NB.  trailing decimal points serve no purpose, just get rid of them

NB.  leading decimal points:   ' .'  ->  ' ..'  ->  ' 0.'

NB.  put it all together, and try and catch pathological cases

If nothing else, since this generates compiled code, the result should
be a bit faster.  Also, note that 'hd' and kdt' could be combined, by

behavior for pathological cases (like ' . '), but is maybe a little
nicer, conceptually.

--

The U.S. government went another thousand million dollars into debt today.

Mon, 01 Aug 1994 13:55:44 GMT  Meaning of Curry in J

...
|> Hmm.. mind if I try?

Not at all! What I posted was essentially a translation of routines I
first wrote in flat APL.

|>    NB.  put it all together, and try and catch pathological cases

Looks interesting. I'll try it.

|>
|> If nothing else, since this generates compiled code, the result should
|> be a bit faster....
J does seem to encourage this style of functional programming. I admit
I'm not fluent in it yet, but later stuff I've written is starting to
go that way. I do find the resulting code very obscure, even if I
wrote it! I guess this is just another version of a fortran
programmers reaction to seeing APL, and may go away once I become more
familiar with the language.

--
Sam Sirlin

Tue, 02 Aug 1994 01:32:26 GMT  Meaning of Curry in J
Me:

Sam Sirlin:
Looks interesting. I'll try it.

|> If nothing else, since this generates compiled code, the result
|> should be a bit faster....

J does seem to encourage this style of functional programming. I
admit I'm not fluent in it yet, but later stuff I've written is
starting to go that way. I do find the resulting code very obscure,
even if I wrote it! I guess this is just another version of a
fortran programmers reaction to seeing APL, and may go away once I
become more familiar with the language.

Yes, a big part of reading J is knowing exactly what the symbols mean
in a specific context.  For example, in the above fragment, it helps

serial fashion -- rather like pipe in unix, except that information
flows from right to left, instead of from left to right.

[Also, arrays are "atomic" in j -- at least in current
implementations, you're pretty much guaranteed that your data isn't
going to be chopped up into fragments by multi-tasking.]

But, when you feel the code is too unreadable, the best solution seems
to be to add more names into the code.  That, and often if you make
the code _more_efficient_ the result is clearer.  [I can think of a
couple examples where I could have made my code more efficient, which
I think would have been clearer.  But I'll leave those as an exercise
for someone else.]

--

The U.S. government went another thousand million dollars into debt today.

Tue, 02 Aug 1994 14:28:21 GMT  Meaning of Curry in J

Quote:
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.

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.

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 divertis{*filter*}t, something completely different.
Ackerman's function is defined on the non-negative integers as follows:

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&+

3&ack  -:  2&^&.(3&+)

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

Thu, 04 Aug 1994 13:22:03 GMT

 Page 1 of 1 [ 13 post ]

Relevant Pages