Author: M.L.Gassanenko
(c) M.L.Gassanenko, 1996-98
History:
ver. 1 Published as "C-Style Arrays in Forth" in Forth Dimensions, Volume XVIII, Number 4, November-December 1996, p.15-21.
ver. 2.1 Converted to HTML (19 Dec 1998)
 

Exercises for the Novice

inspired by the article "C-Style Indexing in ANS Forth"

(published in Forth Dimensions (v.XVIII, n.4, Nov-Dec 1996) as "C-Style Arrays in Forth")

Programming language: Forth (ANS Forth, ANSForth, ANS-Forth)


Although this paper is a proposal, it inspires some good exercises for a novice who is already familiar with Forth but does not have much practice.

1. Implement the words []  , []!  , []^  , []LEN in Forth.

2. Implement the word 0ARRAY, ( n -- addr ) which creates an array of n cells initializing it with zeroes. For example,

4 0ARRAY,
must be equivalent to
0 0 0 0  4 ARRAY,
3. Implement the double-cell array words D[]  , D[]!  , D[]^  , D[]LEN and the words DARRAY, and 0DARRAY, .

4. Implement the bit array words (in both Forth or Assembler, the latter is easier).

5. Implement the word BITARRAY, ( bit[n-1] ... bit[0] n -- addr ) which creates a bit array and initializes it.

6. Implement range checking for double-cell arrays. Double-cell arrays must be accessible as both double- or single-cell arrays, and the range checking must work in both cases.

7. Create a range checking scheme for bit arrays.

8. Take the array words as a basis and extend the array concept to allow arrays with indexes starting from an arbitrary number (and not necessarily from 0), i.e. it must be possible to create e.g. an array which indexes would be numbers -5...4. You may use any approach, but do not redefine the word [] and others. Be careful to choose good names.