Re: Need some help understanding array definitions
- From: Elizabeth D Rather <erather@xxxxxxxxx>
- Date: Wed, 11 Jun 2008 10:02:31 -1000
m_l_g3 wrote:
On Jun 10, 1:51 am, Elizabeth D Rather <erat...@xxxxxxxxx> wrote:
So, you don't want to use a defining word inside a colon definition to
*define* a data object,
I want to explain one more thing.
In Forth, VARIABLE defines a global variable.
Analogously, all other defining words define *global*
things (procedures, arrays, etc.)
As in any programming language, it would be a mistake to use
global variables where you need local ones.
Data structures defined with VARIABLE, CREATE, VALUE, CONSTANT, and related words (as well as custom data structures such as ARRAY) are, indeed, all global. Unlike some languages, Forth doesn't discourage defining global data structures, but it's important to understand their proper use. They provide for "persistent" data (that is, data that will be used over some extent of time or by a number of different definitions), as well as space for strings and arrays. Temporary or intermediate values, as m_l_g says, are appropriately kept on the stack. Strings and arrays should be in defined data structures and referenced by address and length (in the case of strings) or address (in the case of arrays) on the stack.
So what about local variables?
They are not needed. Local variables are used to keep values
that exist while the procedure executes. But you do not
necessarily need local variables for this, you can keep
the values on the data stack. (Well, in fact, whether or nor
stack is enough depends on the *number* of values that you need
access to.)
So, if you study Forth, do not use variables, global or local,
use values on the stack where possible.
This is sounding a little more absolute than is appropriate, I think. The newbie Forth programmer tends to define too many variables, because the stack is unfamiliar and uncomfortable. Therefore, we try to encourage stack use. But there are definitely times when defining a variable or data structure will simplify your code, and then you shouldn't hesitate.
I don't recommend that Forth newbies use local variables. In the first place, their implementation varies across different versions of Forth (there is a standard, but many implementors do it differently). In the second place, it can become a crutch that keeps you from learning good factoring and stack management skills.
Cheers,
Elizabeth
--
==================================================
Elizabeth D. Rather (US & Canada) 800-55-FORTH
FORTH Inc. +1 310.999.6784
5959 West Century Blvd. Suite 700
Los Angeles, CA 90045
http://www.forth.com
"Forth-based products and Services for real-time
applications since 1973."
==================================================
.
- References:
- Need some help understanding array definitions
- From: xgeoff
- Re: Need some help understanding array definitions
- From: Elizabeth D Rather
- Re: Need some help understanding array definitions
- From: m_l_g3
- Need some help understanding array definitions
- Prev by Date: Re: assert( (was: what is the fastest way of finding an integer ...)
- Next by Date: Re: Need some help understanding array definitions
- Previous by thread: Re: Need some help understanding array definitions
- Next by thread: Re: Need some help understanding array definitions
- Index(es):
Relevant Pages
|