Newsgroups: comp.lang.apl
Path: watmath!watserv2.uwaterloo.ca!torn!cs.utexas.edu!sun-barr!ames!elroy.jpl.nasa.gov!jato!csi!sam
From: sam@csi.jpl.nasa.gov (Sam Sirlin)
Subject: Re: Beginner J questions: local variables
Message-ID: <1992Sep10.184235.21427@csi.jpl.nasa.gov>
Originator: sam@kalessin
Sender: usenet@csi.jpl.nasa.gov (Network Noise Transfer Service)
Nntp-Posting-Host: kalessin
Organization: Jet Propulsion Laboratory, Pasadena, CA
References:  <1992Sep10.164511.11493@coe.montana.edu>
Date: Thu, 10 Sep 1992 18:42:35 GMT
Lines: 33


In article <1992Sep10.164511.11493@coe.montana.edu>, oususalg@cs.montana.edu (Glassy) writes:
|> 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
Jet Propulsion Laboratory         sam@kalessin.jpl.nasa.gov

