Need some help understanding array definitions
  Home FAQ Contact Sign in
comp.lang.forth only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.forth Profile…
 Up
Need some help understanding array definitions         


Author: xgeoff
Date: Jun 9, 2008 14:00

Hey all,

I am brand new to Forth, and am doing some initial experiments in
trying to create an actual program. I have read perhaps half a dozen
tutorials/books, including Brodie's but am unable to understand my
current issue.

One of the things I am trying to work with are arrays and have created
the following test code (the array definition is from one of the
tutorials I read):
--------------------------------------
: array ( n -- ) ( i -- addr)
create cells allot
does> swap cells +
;

variable testarray

: testarray2
6 array testarray
;
--------------------------------------
Show full article (1.25Kb)
41 Comments
Re: Need some help understanding array definitions         


Author: xgeoff
Date: Jun 9, 2008 14:02

fyi, I am using win32forth as my forth environment.
no comments
Re: Need some help understanding array definitions         


Author: Elizabeth D Rather
Date: Jun 9, 2008 14:51

xgeoff wrote:
> Hey all,
>
> I am brand new to Forth, and am doing some initial experiments in
> trying to create an actual program. I have read perhaps half a dozen
> tutorials/books, including Brodie's but am unable to understand my
> current issue.
>
> One of the things I am trying to work with are arrays and have created
> the following test code (the array definition is from one of the
> tutorials I read):
> --------------------------------------
> : array ( n -- ) ( i -- addr)
> create cells allot
>> swap cells +
> ;
>
> variable testarray
>
> : testarray2 ...
Show full article (2.17Kb)
1 Comment
Re: Need some help understanding array definitions         


Author: Marcel Hendrix
Date: Jun 9, 2008 16:53

Elizabeth D Rather forth.com> writes Re: Need some help understanding array definitions
> xgeoff wrote:
[..]
>> : testarray2
>> 6 array testarray
>> ;
> Ok, this is a very common beginner error.
> What you have to understand is that words like 'array' and 'variable'
> are defining words just like ':' is. There are many defining words in
> Forth. 'create' is another one.
> Like most Forth words, when you use them in a colon definition, they
[.. fine explanation ..]

It is possible that xgeof is thinking that he can use array to define *local*
arrays, like in some other languages.

This is not possible in standard Forth, although many implementations
allow it in some form.
Show full article (0.91Kb)
no comments
Re: Need some help understanding array definitions         


Author: m_l_g3
Date: Jun 10, 2008 16:42

On Jun 10, 1:51 am, Elizabeth D Rather forth.com> 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.

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.
Show full article (0.97Kb)
no comments
Re: Need some help understanding array definitions         


Author: dkelvey
Date: Jun 11, 2008 07:21

On Jun 9, 2:00 pm, xgeoff gmail.com> wrote:
> Hey all,
>
> I am brand new to Forth, and am doing some initial experiments in
> trying to create an actual program.  I have read perhaps half a dozen
> tutorials/books, including Brodie's but am unable to understand my
> current issue.
>
> One of the things I am trying to work with are arrays and have created
> the following test code (the array definition is from one of the
> tutorials I read):
> --------------------------------------
> : array ( n -- ) ( i -- addr)
>      create cells allot
>      does> swap cells +
> ;
>
> variable testarray
>
> : testarray2 ...
Show full article (2.17Kb)
no comments
Re: Need some help understanding array definitions         


Author: Elizabeth D Rather
Date: Jun 11, 2008 13:02

m_l_g3 wrote:
> On Jun 10, 1:51 am, Elizabeth D Rather forth.com> 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.
Show full article (2.76Kb)
no comments
Re: Need some help understanding array definitions         


Author: Elizabeth D Rather
Date: Jun 11, 2008 13:15

dkelvey@hotmail.com wrote:
...
> If you'd defined "array" to have a compile time
> action, by using the word "immediate" after defining
> it, it would have read the next word in your : definition
> and compiled it. It would also have given you a warning
> that you'd already defined "testarray".

Yes, and it would cause all kinds of grief by trying to make a
definition within a definition. Defining words should *not* be
immediate, and folks shouldn't be encouraged to think that they can make
locals simply by making defining words immediate. I'm sure that's not
what you meant, but it could be read that way.
> Getting an understanding of the destinction between
> compile and execute is probably the hardest to understand
> for someone that has used other languages. It is also
> what makes Forth so unique in that it gives the programmer
> the access to such tools.
Show full article (2.39Kb)
no comments
Re: Need some help understanding array definitions         


Author: Ed
Date: Jun 11, 2008 22:22

"Elizabeth D Rather" forth.com> wrote in message
news:88idnbPPNOfxr83VnZ2dnUVZ_gydnZ2d@supernews.com...
> dkelvey@hotmail.com wrote:
> ...
>> Marcel stated that it is not possible to create local
>> arrays but that is not true. With care, one can create
>> such arrays on the return stack or in an area of
>> memory allocated for such local arrays. One would
>> need to either have error checking or care when
>> using so as not to over use such space.
>> One could even setup a dynamicaly allocated spaces,
>> such as other languages do, with garbage collection and
>> such.
>
> Yes, but these are definitely advanced strategies, and rarely
> appropriate even then. The most advanced Forth programmers I know avoid
> doing these things.

But what to do when they're not avoidable?
Show full article (1.51Kb)
1 Comment
Re: Need some help understanding array definitions         


Author: Marcel Hendrix
Date: Jun 11, 2008 23:45

"Ed" invalid.com> Re: Need some help understanding array definitions
> "Elizabeth D Rather" forth.com> wrote in message
> news:88idnbPPNOfxr83VnZ2dnUVZ_gydnZ2d@supernews.com...
[..]
>> Yes, but these are definitely advanced strategies, and rarely
>> appropriate even then. The most advanced Forth programmers I know avoid
>> doing these things.
> But what to do when they're not avoidable?
[..]

Use the Force :-)

-marcel

-- ---------------------------------------------------------------
\ Based on Wil Baden's idea presented at FORML 1992.
\ The idea is to have a fixed number of variables with fixed names.

ANEW -localbuf

\ : PLACE PACK DROP ;

4 CONSTANT #bufs
#256 CONSTANT /buf

: (frame) ( -- n ) /buf * CHARS ALLOT ;
Show full article (1.66Kb)
no comments

RELATED THREADS
SubjectArticles qty Group
how to join array into arraycomp.lang.perl.misc ·
1 2 3 4 5