home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / tutor / l2p030 < prev    next >
Text File  |  1990-07-15  |  2KB  |  69 lines

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 2 Part 030  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5. By now you should be getting used to Forth's parameter stack.  What have
  6. you been doing when you have made a mistake and ended up with  a lot of
  7. numbers on the parameter stack that you would like to remove?  Perhaps
  8. you entered:
  9.  
  10. 11 22 33  DUP ROT DUP ROT <enter> ok
  11.  
  12. and now you have 5 numbers on the stack (what are they?).  How do you
  13. clear the stack so that it is empty (no numbers on the stack)?
  14.  
  15. Method 1:  . . . . . <enter>    ( just print till stack is empty)
  16.  
  17. Method 2:  CLEAR <enter> CLEAR <- What?
  18.  
  19. That's because there is no word CLEAR! but notice that it did clear the
  20. parameter stack.  We could clear the parameter stack by entering any
  21. Forth word that has not already been defined.  This is not a very
  22. sophisticated solution but it works.  You might like method 3 better.
  23.  
  24. Method 3: Make a word to clear the stack.  Here is the definition.
  25.  
  26. : CLEAR ( ?? -- "empty stack" ) \ Clear the parameter stack
  27.        DEPTH 0 ?DO DROP LOOP ;  ok
  28.  
  29. Sample of its use:
  30. 11 22 33 .S  [3]     11      22      33  ok
  31. CLEAR .S  Stack Empty.  ok
  32.  
  33. ╓──────────────╖
  34. ║ Problem 2.2  ║
  35. ╙──────────────╜
  36. Why did we use ?DO in the definition of CLEAR instead of DO ?
  37. If you don't know the answer here is another question. What happens if
  38. both the loop limit and initial value for a DO loop are identical?
  39.  
  40. Try this:      : TEST1 0 0  DO I . LOOP ;  TEST1 <enter>
  41. Now try this:  : TEST2 0 0 ?DO I . LOOP ;  TEST2 <enter>
  42.  
  43. You should now have the answer to the first question asked.
  44.  
  45. Now use Editor to enter the following Counting program.
  46. NEWFILE COUNTING <enter>
  47.  
  48. \ Loop Demo program that counts from 0 up to n.
  49. : COUNT_UP ( n -- )  0 ?DO   CR I .   LOOP ;
  50.  
  51. ╓──────────────╖
  52. ║ Problem 2.3  ║
  53. ╙──────────────╜
  54. 1) Why does the ?DO  have only one stack input?
  55. 2) What is the function of the word CR ?
  56. 3) What is the function of the word I  ?
  57. 4) Why  does the program count up?
  58. 5) What happens if COUNT_UP is executed without a stack input?
  59. 6) What would happen if the  " . " was accidentally left out and
  60.    COUNT_UP was tested with 100 as the stack input?
  61. 7) How high does the program count?
  62.  
  63. ┌─────────────────────────────────────┐
  64. │  Please move to Lesson 2 Part 035   │
  65. └─────────────────────────────────────┘
  66.  
  67.  
  68.  
  69.