If you mean this with all possible sums and subtractions in the J-ish way:

   1+2+3
6
   1+2-3
0
   1-2+3
_4
   1-2-3
2

then the number of all possible +- combinations is:  2^ <:# 1 2 3
So a small list of 30 elements gives already
   2^<:30
536870912
+- configurations

What's going on? The insert / places verb (+,-) between the array elements:

   1(+,-)4(+,-)9

Working from right to left:

   4(+,-)9
13 _5

   1(+,-)13 _5
14 _4 _12 6

The verb (+,-) is a so called fork, meaning:
   x (u v w) y
   (x u y) v (x w y)
Giving:
   (x+y),(x-y)


To make this a verb v: one possible way is using

v=:3 :'/:~ ~. | (+,-)/y'

If you want a tacit-version use 13 instead of 3 in the definition:

v=:13 :'/:~ ~. | (+,-)/y'

and 'ask' for v:

   v
[: /:~ [: ~. [: | (+ , -)/

giving

v=: [: /:~ [: ~. [: | (+ , -)/
 
What could be converted e.g. to (less ugly?)

v=: [:/:[EMAIL PROTECTED]|@(+,-)/

   v 1 2 3
0 2 4 6

See on this subject e.g. 
http://www.jsoftware.com/help/primer/explicit_tacit_trans.htm
Also: Oleg Kobchenko has a nice tiny lesson in tacit programming. But I
can't remember where I read that.

For me this is also a kind of lesson.  :-)



Geoff Canyon schreef:
> I'm trying to take a set of numbers and find a list of all the
> possible (positive) results of adding or subtracting those numbers. So
> for 1 4 9 the result would be 4 6 12 14.
>
> I spent some time trying to figure out how to come up with a verb that
> could be inserted between each atom of the list that would get the
> positive and negative of the argument to its right, add that to each
> item of the list to its left, get the nub of that list, and then
> continue on, but that didn't work.
>
> Then I happened to try (+,-)/ to see what it would do, and low and
> behold it seemed to generate most of the possible values (?) For example:
>
>    (+,-)/ 1 2 3
> 6 0 _4 2
>
> I have no idea what is going on here. Can anyone explain? In short
> sentences of small words ;-)
>
> The train I ended up with is shown below. It seems to work, but is it
> right?
>
>    /:~ ~. | (+,-)/ 3 6 11 12
> 2 4 8 10 14 20 26 32
>
> Also (sorry to still be tossing out newbie questions) if this is
> right, how do I package it up so I can use it:
>
>    v=: /:~ ~. | (+,-)/
>    v 1 2 3 4
> |length error: v
>
> Thanks -- still having fun!
> ----------------------------------------------------------------------
> 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