ForthNet@willett.UUCP (ForthNet articles from GEnie) (01/05/90)
Date: 12-23-89 (17:19) Number: 2706 (Echo)
To: ALL Refer#: NONE
From: STEVE PALINCSAR Read: (N/A)
Subj: SORTS and "I" - pt3 Status: PUBLIC MESSAGE
The biggest difficulty I experienced in translating the texts
to Forth was related to the fact that in BASIC, Pascal, etc.,
the "I" and "J" used to refer to loop indexes are variables.
I, wherever iit appears, refers to the loop index in the outer
loop; J to the index of the loop inside it; etc. Not so with
Forth! . In the loop in SELECTION_SORT, every fetch of a loop
index is made with I -- but thhe "I" bracked with => <= is the
index of the outer loop, and the I bracked with ==>> <<== is
the index of the inner loop. What's going on here?
.
: SELECTION_SORT
COUNTER 0 DO
=> I <= smallest! \ Outer loop index
COUNTER => I <= DO \ Outer loop index
smallest ELEMENT ==>> I <<== ELEMENT larger? \ inner
IF ==>> I <<== smallest! THEN \ loop index
LOOP
smallest ELEMENT => I <= ELEMENT {exchange} \ outer loop index
LOOP ;
.
Forth's "I" puts the index of a DO loop on the stack, but it
does it by fetching not from a variable, but from the top of
the return stack. If you nest a loop inside of the first
loop, the limit and index of the inner loop go on top of the
index of the outer loop. The value that I finds on top of the
Return stack is the index of the inner loop. Inside the body
of the inner loop, to get access to the index of the outer
loop, you must use J. Once past the "LOOP" of the inner loop,
its index and limit are removed from the return stack, and the
value that I finds is the index of the outer loop once again.
-----
This message came from GEnie via willett through a semi-automated program.
Report problems to: 'uunet!willett!dwp' or 'willett!dwp@gateway.sei.cmu.edu'