All Sorts of Sorts 
Author Message
 All Sorts of Sorts

Category 3,  Topic 37
Message 33        Sun Mar 11, 1990
F.SERGEANT [Frank]           at 10:41 CST

 Ian, you asked about nested FOR-NEXT loops.  Suppose we have a two
dimensional array of quarterly net profit figures for the years 1970 thru
1975.  Suppose further that we made very small profits (so they will  fit in
16-bits).  We'll hard-code the values to start with.

 CREATE  PROFITS
 ( year   1stQ   2ndQ   3rdQ  4thQ  )
 ( 1970)  10 ,   5 ,    3 ,   7 ,     ( each entry takes 2 bytes)
 ( 1971)   8 ,   9 ,   -3 ,  -4 ,     ( each year takes 8 bytes)
 ( 1972)   2 ,   3 ,    4 ,   8 ,
 ( 1973)   6 ,   6 ,    6 ,   6 ,
 ( 1974)   5 ,   3 ,    3 ,  -7 ,
 ( 1975)  12 ,  11 ,    9 ,  10 ,

 We can address the array various ways.  Suppose we want to say
    3rdQ 1972 PROFIT
 and thereby place the number 4 on the stack.  We could do it with the
following definitions:

    1 CONSTANT 1stQ
    2 CONSTANT 2ndQ
    3 CONSTANT 3rdQ
    4 CONSTANT 4thQ

    : PROFIT  ( quarter  year  -  n)
      1970 -  8 *  ( quarter  offset-to-start-of-the-year)
      1- SWAP 2* +    ( offset-to-quarter)
      PROFITS      ( offset-to-quarter  starting-addr-of-the-array)

  In memory, if you were to dump the contents of the array, on a PC  with
Intel-like byte order, you would see

  HEX PROFITS  DUMP  DUMP  DUMP    ( one way to do it in Pygmy)
 XXXX  0A 00  05 00  03 00  07 00    08 00 09 00 FD FF FC FF
 XXXX  02 00  03 00  04 00  08 00    06 00 06 00 06 00 06 00
 XXXX  05 00  03 00  03 00  F9 FF    0C 00 0B 00 09 00 0A 00

 (The XXXXs stand for the actual addresses where the array is stored.)

  Now let's step thru the entire array using FOR-NEXT loops.

  : PRINT-PROFITS  ( -)  
    CR  ." Quarterly Profits for 6 Years"  CR CR
    1970  ( starting-year)
    5 FOR ( we'll do this outer loop 6 times)
      1   ( starting-quarter)
      3 FOR ( we'll do this inner loop 4 times)
          ( year  quarter)   2DUP SWAP  ( y q q y)
          PROFIT  ( y q profit)  .  ( y q )
          ( year  old-quarter)  1+  ( year  new-quarter)
      NEXT  ( old-year  old-quarter)  
      DROP  ( old-year)  1+  ( new-year)
      CR  ( so each year will get a separate line)
    NEXT
    DROP   (   )  ;

   Notice that I have used nested loops, but I haven't used the built  in
"control variables" named 'I' and perhaps 'J'.  But, I have used  control
variables that are kept on the stack.  

   The above should work in Pygmy ver 1.2 and with most Forths that  have FOR-
NEXT.  My latest thinking is that I prefer 5 FOR to do it 5  times, not 6, so
I have changed my copy of Pygmy to do it the new way.   Robert Berkey thinks
we should call the new word ?FOR.

   Pygmy has the word 'I' that returns the value of the built-in index  in the
current loop.  This index counts down.

    : TST1  5 FOR  I .  NEXT  ;   TST1  would print  5  4  3  2  1  0
    : TST2  5 FOR  I .  NEXT  ;   TST2  would print  4  3  2  1  0

    Also you can't directly get to the built-in index of outer loops  from
within inner loops (but, you can get it from the outer loop while  just in the
outer loop and save it on the stack for later use by the  inner loop).  This
facility could be built into Pygmy (named 'J' 'K'  etc) just as it is built in
to some  DO-LOOP implementations.  Because  of the downward counting nature,
you might not always want to use 'I'  in a FOR-NEXT loop.  

     Here is a re-write using DO-LOOP where J accesses the outer loop's
index:

  : PRINT-PROFITS2 ( -)  
    CR  ." Quarterly Profits for 6 Years"  CR CR
    1976 1970 DO ( we'll do this outer loop 6 times)
      5 1 DO ( we'll do this inner loop 4 times)
          I J PROFIT  ( profit)  .  (   )
      LOOP  
      CR  ( so each year will get a separate line)
    LOOP   (   )  ;

     I've presented all of this in hopes of making it clear how it all works
rather than to convince you that FOR-NEXT is better than DO-LOOP. Even I have
to admit that the example with DO-LOOP looks more straight  forward.  I just
immediately switched to FOR-NEXT loops and never use  DO-LOOP loops any more.
I'm pointing this out as a curiosity as I  don't fully understand why I've
done it, but am content with the  situation.  Naturally I haven't tested any
of the above examples.
  -- Frank
-----
This message came from GEnie via willett through a semi-automated process.



Fri, 28 Aug 1992 12:01:20 GMT  
 All Sorts of Sorts

 Date: 03-12-90 (10:12)              Number: 3020 (Echo)
   To: FRANK SERGEANT                Refer#: 3017
 From: IAN GREEN                       Read: NO
 Subj: ALL SORTS OF SORTS            Status: PUBLIC MESSAGE

    Well, you're latest examples have clarified one aspect of Forth; it
 lacks standardization of even the most basic control structure. To this
 end it appears as though translation of existing code may not be too
 easy to achieve as the looping control structures do not behave in a
 regular and predictable mannor. Oh well.

 Ian Green

 NET/Mail : British Columbia Forth Board - Burnaby BC - (604)434-5886  
-----
This message came from GEnie via willett through a semi-automated process.



Sun, 30 Aug 1992 08:34:45 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Sort (List sorts...)

2. Binary Sort/Merge Sort in awk

3. Sort of Sorts

4. All Sorts of Sorts

5. All Sorts of Sorts

6. All Sorts of Sorts

7. Bubble sort/Selection sort help neded.

8. Qsort, Merge sort, Radix sort in prolog Help!

9. UNIX sort exponents (bc|sort)

10. ANN: New release of S.C.A. Micro Templates (Browse Header Sort and Reverse Sort Template)

11. Straight Insertion Sort and Binary Insertion Sort

12. Sorting a file w/o SORT verb

 

 
Powered by phpBB® Forum Software