Here are 3 equivalent ways:

   a=. 1 2 3 4 5
   b=. 1 2
   +/ , a +/ b
45
   +/ , a +"_ 0 b
45
   +/ , a +"0 _ b
45
    
Verb rank is the key to what you are trying to learn.


Henry Rich

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Ian Gorse
> Sent: Sunday, August 03, 2008 7:43 PM
> To: [email protected]
> Subject: [Jprogramming] learning: Having trouble writing 
> loopless scripts
> 
> Hi,
> 
> I know several programming languages, but I am still new to 
> J, and the whole
> style of programming is alot different to what I am used too, but I am
> finding it extremely interesting, as it makes me think of 
> ways to solve
> problems differently to what I normally do.
> 
> I keep running into the same problem over and over again 
> (probably because I
> have spent so long with 'standard' programming languages), and that is
> writing a script that doesn't use explicit loops.
> 
> Even though I have solved the problem I was trying to solve 
> in J, I would
> like to do it without using a loop like I did.
> 
> I have simplified my problem to try and explain what I am 
> trying to do.
> 
> a=. 1 2 3 4 5
> b=. 1 2
> 
> I want to be able to add each of the values in 'b' with each 
> of the values
> in 'a', then sum them up
> 
> 
> for example
> (a[0]+b[0])  + (a[1]+b[0])  + (a[2]+b[0])  +(a[3]+b[0])  + 
> (a[4]+b[0]) ) = 2
> 3 4 5 6 = 20
> which is (1+1) +  (2+1) +  (3+1) + (4+1) + (5+1) = 20
> 
> (a[0]+b[1])  + (a[1]+b[1])  + (a[2]+b[1])  +(a[3]+b[1])  + 
> (a[4]+b[1]) ) = 3
> 4 5 6 7 = 25
> which is (1+2) +  (2+2) +  (3+2) + (4+2) + (5+2) = 25
> 
> total = 20 + 25 = 45
> 
> 
> The way I solved it was with
> 
>    loop =. 4 : 0
> counter=.0
> sum=.0
> while. counter < #y
> do. sum=. sum + (+/ ((counter { y) + x))
> counter=.counter+1
> end.
> sum
> )
> 
>  a loop b
> 
> but I don't like this method, and would like to find a more 
> direct approach
> without using the program control flow
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
> 

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

Reply via email to