On Wed, Aug 6, 2008 at 3:07 PM, Ian Gorse <[EMAIL PROTECTED]> wrote:
> c=: 4 : '(!(x+y)) % (x+y)'
> a=: 1 2 3 4 5
>
> and the result I want are
>   a c 1
> 1 2 6 24 120
>   a c 2
> 2 6 24 120 720
>   a c 3
> 6 24 120 720 5040

In this case, you have defined your verb to require atomic
(single number) right arguments.  You could express this
in your definition using the rank conjunction:
   c3=: 4 : '(!(x+y)) % (x+y)'"_ 0
   a c3/1 2 3

Or, you might instead say that both the
left and the right arguments must be atoms:
   c4=: 4 : '(!(x+y)) % (x+y)'"0
   a c4/1 2 3

(Note that these results are structured slightly differently
because they have defined "items" of a differently).

But note that you have several operations here:
First, you need to sum the left and right arguments.  You
do not care about them individually once you have their
sum.  Then, you want to divide the factorial of that sum
by the sum itself.  So you could instead define
   c5=: 3 :'(!y)%y'@+
   a c5/1 2 3

Notice that I do not have to explicitly use rank any longer
because + has 0 for its ranks, and when I use @ to derive
a new verb, @ uses the rank of the verb on its right for the
rank of its new verb.

And we could simplify this further -- getting rid of the
parenthesis -- by using %~ instead of %.  If you do not
feel like looking up how ~ works,  m%n is equivalent to
n%~m.  (~ reverses the order of arguments to its verb.)
   c6=: 3 :'y %~ !y'@+
   a c6/1 2 3

Now... for hooks...  when I have two verbs, f2 and f1 in
an expression like y f2 f1 y, I can restate that as (f2 f1) y.
This might seem like a trivial thing -- why bother with
this restatement?  But when I do this, I have factored out
y, so I do not need to bother with y any longer:
   c7=: (%~!)@+
   a c7/1 2 3

I could also have incorporated / into the definition of the
verb itself, but that would be a problem if I every wanted
to pair up related left and right arguments.


FYI,

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

Reply via email to