On Nov 9, 2010, at 2:08 PM, Shaping wrote:

>  
> I'm hungry for local variables, now, though this goes against much of the 
> basis of stack languages.  I want lexical forms I can read and understand 
> (parse with my mind, left-to-right, and down in a tree) to produce a known 
> result (not a best guess).  I don't want to remember a lot of stuff, because 
> human memory is fragile and ultimately limits all programmers.  I know that 
> we have a "locals" vocab of some kind, but don't know how to use it.  Is 
> anyone using locals a lot in Factor just for readability?  Can locals be used 
> efficiently for the machine, too?  How can locals be abused?

Locals are used all over the place, and suffer no performance penalty. Don't 
worry about using them, or "going against the basis of stack languages." 
Practicality beats purity. Often, after writing something out with locals, it 
makes the concatenative approach clearer.

You can bind local variables by using the "::" definition form, which binds the 
function's input values to lexical variables:

:: add ( x y -- z ) x y + ;

Within a :: definition, you can bind additional variables off the top of the 
stack with the ":>" operator:

:: quadratic ( a b c -- x1 x2 )
        2 a * recip :> 1/2a
        b neg 1/2a * :> -b/2a
        b sq 4 a c * * - sqrt 1/2a * :> disc/2a
        -b/2a disc/2a +
        -b/2a disc/2a - ;

-Joe

------------------------------------------------------------------------------
The Next 800 Companies to Lead America's Growth: New Video Whitepaper
David G. Thomson, author of the best-selling book "Blueprint to a 
Billion" shares his insights and actions to help propel your 
business during the next growth cycle. Listen Now!
http://p.sf.net/sfu/SAP-dev2dev
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to