Beginner J questions: local variables
Author Message
Beginner J questions: local variables

|> Here's a verb to find the arithmetic mean of a rank-1 vector:
|> bar =. '(+/ y.) % (# y.)' : ''
|>
|> ok.  Now I'd like to write a verb to calculate the variance
|> of a vector.  The following does the trick:
|>
|> var =. 'bar *: y. - bar y.' : ''
|>
|> what i'd like to do, is encapsulate bar y. in a noun local
|> to the var verb.  var is correct and straightforward, but it
|> irks me that it recalculates bar y. repeatedly when it doesn't have
|> to.

You mean that it has to compile bar twice? Or just call bar twice?

|> can local variables be embedded in verb definitions?
|> if so, how?

If I understand what you want, you can do it with tacit definition and
fix. Check out these (and other variants I'm sure you can come up
with).

var1 =. 'bar *: y. - bar y.' : 11 f.

bart =. '(+/ y.) % (# y.)' : 11
vart =. 'bart *: y. - bart y.' : 11 f.

--
Sam Sirlin

Tue, 28 Feb 1995 02:42:35 GMT
Beginner J questions: local variables
Hi J-folk:

Per subj: line, I have some questions on local variables in J.

Here's a verb to find the arithmetic mean of a rank-1 vector:

bar =. '(+/ y.) % (# y.)' : ''

ok.  Now I'd like to write a verb to calculate the variance
of a vector.  The following does the trick:

var =. 'bar *: y. - bar y.' : ''

what i'd like to do, is encapsulate bar y. in a noun local
to the var verb.  var is correct and straightforward, but it
irks me that it recalculates bar y. repeatedly when it doesn't have
to.  can local variables be embedded in verb definitions?
if so, how?

(i don't *know* that var recalculates bar y.  just a guess.)

lou.
--

Tue, 28 Feb 1995 00:45:11 GMT
Beginner J questions: local variables
Lou Glassy:
.  bar =. '(+/ y.) % (# y.)' : ''
.  var =. 'bar *: y. - bar y.' : ''

.  what i'd like to do, is encapsulate bar y. in a noun local to the
.  var verb.  var is correct and straightforward, but it irks me that
.  it recalculates bar y. repeatedly when it doesn't have to.

You can encapsulate 'bar y.' in a noun local to the verb 'var', but
this value does not appear to be reused.  'bar' is used twice, but it
is being used on two different values.  For example:

var =. 'bar *: y. - local=. bar y.' : ''

...............................................................................

By the way, to revert back to a completely unrelated thread, you can
do on-the-same-line prompting on the PC if you invoke j without the
session manager.  As follows:

write =. 1!:2
prompt =. write & (<'CON')              NB. 'CON' must be capitalized

then try:   input 'Foo: '

If you've invoked j with the /s option, you will see the prompt.

--

Tue, 28 Feb 1995 10:15:45 GMT
Beginner J questions: local variables

Quote:
Lou Glassy writes:
> bar =. '(+/ y.) % (# y.)' : ''
> var =. 'bar *: y. - bar y.' : ''

> what i'd like to do, is encapsulate bar y. in a noun local
> to the var verb.  var is correct and straightforward, but it
> irks me that it recalculates bar y. repeatedly when it doesn't have
> to.  can local variables be embedded in verb definitions?
> if so, how?

> (i don't *know* that var recalculates bar y.  just a guess.)

var calculates bar y. just once.  In any case, names can be assigned
within explicit verbs as without, using =. or =: .  For example:

s2 =. '((n*+/*:y.)-*:+/y.)%*:n=.#y.' : ''

------------------------------------
Roger Hui, Iverson Software Inc., 33 Major Street, Toronto, Ontario  M5S 2K9
Phone: (416) 925 6096;  Fax: (416) 488 7559

Wed, 01 Mar 1995 13:57:06 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages