Geoff
NB. I've added some extra line throws to avoid line-wrap
Yes it is!
You already seem to be doing what you require, so you don't
really have a problem. Anyway,
(+ , -)
is a fork, ie a train of three verbs, (f g h) and we're told,
in, for example, ~help/primer/fork.htm
that a dyad x (f g h) y is evaluated as
(x f y) g (x h y)
So
1 (+ , - ) 4
5 _3
In short sentences, it does what it says; you can read
x (+, -) y
as
append (the difference of x and y) to (the sum of x and y)
An easy - and instructive - way to package it up is to try
13 : quoted explicit expression, as in
13 : '/:~ ~. | (+,-)/ y'
[: /:~ [: ~. [: | (+ , -)/
( [: /:~ [: ~. [: | (+ , -)/ ) 1 4 9 NB. eg
4 6 12 14
FWIW, the form I came up with is
/:~ @ ~. @: | @ (+,-) /
since I tend to use "at" and "atop" rather than "cap" . You
might enjoy investigating why they produce the same result.
I find it helps to tick
edit/configure/display/display form: linear+box+allow multiple
The boxed display helps you to recognise forks and hooks.
This for example demonstrates that your v includes a (monad) fork
(~. | ((+,-)/) ) NB. f <=> ~. g <=> | h <=> (+,-)/
which would evaluate as
(~. y) | (+,-)/ y
and, in general, and in your particular case,
~. y is not the same length as (+,-)/ y
There might well be a more efficient version of the verb for large
arguments, but that's not your goal here, I think.
Hoping you continue having fun
Mike
Geoff Canyon wrote:
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