Sean McGowan writes:
> I'm new to J and have a question regarding tacit functions. I need
> to write tacit a J function that takes a list of 3 numbers as its
> argument. How do you pass and reference the elements of a list in
> a function?
...
> list =: 10 0 1
> N =: 0 { list
> A =: 1 { list
> B =: 2 { list
One simple approach replaces N, A, and B with functions which
would extract the desired values from their argument:
N=: 0 { ]
A=: 1 { ]
B=: 2 { ]
Note that I'm using j601 beta. If you're using j504, you'll
need to explicitly convert each of the above numbers to constant
functions. N=: 0: { ] or N=: 0"_ { ] and so on...
Once you have these data selection functions, their use is
fairly obvious:
(A, B, N) list
0 1 10
(A + B * N) list
10
Typically, you can replace a name with a parenthesized expression
containing the value assigned to a name, and vice versa.
expr=: A + B * N
expr list
10
Exception: this equivalence with names does not hold on the left
hand side of an assignment operator.
(A + B * N)=: A + B * N
|syntax error
Also, f. will extract expressions from names.
expr f.
(1 { ]) + (2 { ]) * 0 { ]
Note that you could also use the 13 : translator
13 : '(1{y) + (2{y) + (0{y)'
(1 { ]) + (2 { ]) + 0 { ]
Again, note that I'm using j601 beta. If you're using
j504, you'll need to replace y with y. in that 13 :
expression.
Finally note that this use of selection functions tends
to encourage you to re-use your argument structure throughout
your application.
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm