[Jprogramming] FW: A simple function
Linda Alvord
lindaalvord at verizon.net
Sun Nov 27 11:56:25 HKT 2011
Here you have the code that creates the result. Copy and paste and execute
it and you should get a readable tree diagram of the verb.
f=: 13 :'<.0.5+x*%:x*y'
f
[: <. 0.5 + [ * [: %: *
Or you can now write a tacit definition with the above result
g=:[: <. 0.5 + [ * [: %: *
4 f 17
33
4 g 17
33
5!:4 <'f'
-- [:
+- <.
--+ -- 0.5
│ +- +
L----+ -- [
│ +- *
L-----+ -- [:
L---+- %:
L- *
Linda
-----Original Message-----
From: programming-bounces at jsoftware.com
[mailto:programming-bounces at jsoftware.com] On Behalf Of EelVex
Sent: Saturday, November 26, 2011 9:15 AM
To: matxinlekim at hotmail.com
Cc: Programming forum
Subject: Re: [Jprogramming] FW: A simple function
On Sat, Nov 26, 2011 at 11:29 AM, m l <matxinlekim at hotmail.com> wrote:
>
> I`m a beginner in J programming.
> I need writing a simple function:
>
> round ( x * sqrt (x*y))
>
> Thanks in advance.
>
> Matxin
>
>
Among the gazillion ways to do this:
f =: [: <.0.5+ {. * %:@*/
f 15 30
318
------
1. If you have a list of two numbers: x,y, you multiply them with `*/`:
*/ 15 30
450
2. You then take the square root (`%:`) of (`@`) that:
(%: @ */) 15 30
21.2132
3. and multiply that by the first term of your list (`{.`)
({. * (%: @ */)) 15 30
318.198
4. You then "apply" (`[:`) rounding (`<.0.5+`) to that:
([: <.0.5+ {. * (%:@*/)) 15 30
318
Note that the construct in step 3 is a train of 3 verbs (see:
http://www.jsoftware.com/help/learning/09.htm).
(f g h) y == (f y) g (h y)
where
y = 15 30
f = {. (take first)
g = * (multiply)
h = %:@*/ (square root of product)
Also, you round a number n by taking the floor of (n + 0.5).
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list