Newsgroups: comp.lang.apl
Path: watmath!watserv2.uwaterloo.ca!torn!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!darwin.sura.net!wupost!usc!elroy.jpl.nasa.gov!jato!csi!sam
From: sam@csi.jpl.nasa.gov (Sam Sirlin)
Subject: Re: what about func(a,a,a)
Message-ID: <1992Dec3.170007.16358@csi.jpl.nasa.gov>
Originator: sam@kalessin
Sender: usenet@csi.jpl.nasa.gov (Network Noise Transfer Service)
Nntp-Posting-Host: kalessin
Organization: Jet Propulsion Laboratory, Pasadena, CA
References:  <1992Dec3.120841.9774@bernina.ethz.ch>
Date: Thu, 3 Dec 1992 17:00:07 GMT
Lines: 98


In article <1992Dec3.120841.9774@bernina.ethz.ch>, hoesel@igc.ethz.ch (Frans van Hoesel) writes:
|> So I'm writing another language that does array processing (don't blame
|> me for that please). 

Without any implicit blame I'd still like to ask why.

|> Suppose that a user write[s] a function myfunc
|> int[] myfunc(int a[], int b[])
|> the function is expected to work with 1D arrays. 

How is this specified? This is a hint towards a solution.

|> If it is called
|> with a 2D array, then the compiler will generate an implied loop
|> repeatedly calling the function with all the rows in the
|> 2D array. What must happen if I call the function with both arguments
|> being a 2D array?
|> Should one expect  a loop over a loop, or just one loop running in
|> parallel over both the arguments?, what if the shape of the arrays
|> isn't the same?

All of the above, of course! What you want is the concept of rank in
J. I'd recommend you get the documentation, since $30 or so isn't too
much of an investment when you're building a new language. 

Briefly, functions can have lots of different "natural" ranks, from 0
to infinite. For example a matrix inverse function naturally operates
on rank 2 objects (matrices), whereas + operates on scalars (0 rank).
A neat thing about rank in J is that it is adjustable. This requires
the pretty-much-unique-to-APL (though I suppose other languages could
do it I don't think it's used much) concept of an operator or adverb:
a function (in the mathematical sense) that takes as data other
functions and returns a new function. In J it's (").

Here's an example along the lines of your question. Define an inner
product function, which makes sense only for rank 1 things. 

   i=. +/ . *
   1 2 i 3 4
11

(hopefully you are aware that APL/J use user functions the same way as
built-in functions, for example 1 2 + 3 4 etc, and that functions in J
have one argument on the left - monadic - or two arguments on left and
right - dyadic).  Notice that J does some expected extension:

   m=. 2 2$1 2 3 4
   m
1 2
3 4
   1 5 i m
16 22
   n=. 1+%m
   n
      2  1.5
1.33333 1.25
   m i n
4.66667   4
11.3333 9.5

But I can change the behaviour if I want. For example:

   m i"0 n  NB. inner product for scalars is just the ordinary product
2 3 
4 5
   m i"1 n
5 9
   m i"2 n
4.66667   4
11.3333 9.5

Of course all verbs (functions that act on nouns [numbers or
characters]) have a monadic, left, and right rank, so I can actually
specify these separately, for example:

   m i"0 1 n
 3.5       7
7.75 10.3333

But hopefully you get the idea. I've found this very helpful in
practice. 

Oh yes, and the expected dimensions of the arguments must agree of
course: 
   1 2 i 3 4 5
length error
   1 2 3 i m
length error

-- 
Sam Sirlin
Jet Propulsion Laboratory         sam@kalessin.jpl.nasa.gov





