If you want to define a function in a way that is closest
to how expressions work, then:

   f=: 3 : '% +/ % y'

is the best choice.  If you prefer to define a function
using compositions (i.e. "tacitly"), then:

   f1=: % @: (+/) @: %

is better.  If you want to emphasize the fact that
you have a dual, then use one of:

   f2=: +/ &.: %
   f2a=: +/ &. (%"_)

More about duals: http://www.jsoftware.com/jwiki/Essays/Under

It is easier to have an algebra of functions (that is,
easier to reason about functions) if they are
defined using compositions.



----- Original Message -----
From: Alex Gian <[email protected]>
Date: Wednesday, February 3, 2010 16:36
Subject: [Jprogramming] A q for starters - composing without forking (% +/ %)
To: [email protected]

> Hi - complete newbie to J here. Though I like what I've seen so far.
> 
> Here's my starter question:
> I'd like to code the function that gives the inverse of a sum of
> inverses (eg. like when calculating resistors in parallel)
> 
> It works fine if I just write it out as
>       % +/ % 10 20 
> 40            (ans 5.71429)
> 
> However, if I try to make a function of it, 
>       invsuminv =. % +/ %
> it becomes a fork (I guess that _does_ make sense), and just computes
> the "cross-sum" of the inverses, as if I'd typed
>            (% 
> +/ %) 10 20 40
>           0.2  
> 0.15 0.125
>          
> 0.15   0.1 0.075
>       0.125 0.075  0.05
> 
> How can I avoid this? I've tried all sorts of combinations, 
> using @ and
> & but can't seem to crack it.  Obviously something 
> fundamental I'm not
> grasping. Any help appreciated.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to