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.)

thanks in advance...

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:

read  =. 1!:1
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  
 
 [ 4 post ] 

 Relevant Pages 

1. local variable and local variable in block behave differently

2. Sequence local = local variable?

3. JS-EAI with *JS*-callback

4. js.exception 3279.js

5. questions about ANSI Forth local variables

6. newbie question about local variables

7. Question about variables local to subroutine

8. Basic question on local variable

9. questions on procedure local variables and namespaces

10. Questions on block local variables

11. Local variables - lifetime and access variables [Q]

12. Beginners Question - use of variables

 

 
Powered by phpBB® Forum Software