home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / Prograph Classic 2.6.1 / Learning Prograph / Simple Examples ƒ / Simple Examples.ReadMe < prev   
Encoding:
Text File  |  1994-10-21  |  11.0 KB  |  250 lines  |  [TEXT/ttxt]

  1. Example:      Simple Examples ƒ
  2. Written by:  Prograph International
  3. Contents:     Simple Examples.pgs
  4.                    Simple Examples.ReadMe
  5.  
  6. Needs Prograph Extensions:
  7.                     Math Primitives
  8.                     More Primitives
  9.                     Primitives
  10.  
  11. Needs Libraries to Compile:
  12.                     Library 2.6
  13.  
  14. Description
  15. -----------
  16. The "Simple Examples.pgs" file is a collection of small, focused Prograph methods that illustrate the language features and some of the more useful primitives supplied with the language.  It is hoped that these can serve as a learning aid for people just becoming familiar with Prograph, as well as a useful review for experienced users.
  17.  
  18. The examples are organized in classes, simply as a device for categorizing them.  The methods in each class are described in detail below.  If a method is denoted as "not directly executable", that means that you cannot simply select the method and choose "Execute Method" from the Exec menu, because the method requires inputs.  Therefore, each of those methods has an example method which calls it, and of course they can be called from your own Prograph code, with the correct inputs.
  19.  
  20. UNIVERSAL
  21.  
  22. • Initial
  23. Repeatedly calls method "execute a method" in class "System Examples" until user cancels from one of the dialog boxes.  This is the Program method for this file, which will execute when you select Run from the Exec menu or when you compile this program.
  24.  
  25. CLASS: Control
  26. These methods illustrate the use of Prograph cases and controls (Next Case, Terminate, etc.) to achieve code structures people may be familiar with from other languages (e.g., if...then...else).  The C equivalent of the  Prograph code is on the comment for each method.  Most of these methods also illustrate the "ask", "answer" and "show" primitives.
  27.  
  28. • if... then... else #1
  29. Uses the "Next case on failure" control in a method with two cases to implement the functionality of "if A then (case 1) else (case 2)".
  30.  
  31. • if... then... else #2
  32. Uses the "Next case on success" control in a method with two cases to implement the functionality of "if not A then (case 1) else (case 2)". 
  33.  
  34. • cases
  35. Uses the "Next case on failure" control in a method with three cases to implement "if A then (case 1) else if B then (case 2) else (case 3)".
  36.  
  37. • nested cases
  38. Uses the "Next case on failure" control in two nested levels of locals to combine the test used in IF...THEN...ELSE #1 and the CASES example.
  39.  
  40. • for loop
  41. Uses the "loop annotation" and a "Terminate on success" control to implement a loop wherein a value gets incremented every time through the loop until it reaches a maximum value and the loop gets terminated.
  42.  
  43. • do..while loop
  44. Uses the "Loop annotation" and a "Finish on failure" control to implement a loop which, when a certain condition is no longer true, executes the contents one more time and then stops looping.
  45.  
  46. • while loop
  47. Uses the "Loop annotation" and a "Terminate on failure" control to implement a loop which, when a certain condition is no longer true, stops looping immediately.
  48.  
  49. • control on the output bar
  50. Illustrates a situation in which putting a "Fail on success" control on the output bar can get you where you want to go.  This technique is often overlooked by experienced Prograph programmers and beginners alike.
  51.  
  52. • propogation of failure
  53. Illustrates the use of "Fail on failure" controls to pass a failure condition up from inside nested locals.  This is another often misunderstood technique which is very useful.
  54.  
  55.  
  56.  
  57. CLASS: Math
  58. Just a few simple examples to help you get started visualizing mathematical problems in Prograph dataflow diagrams.
  59.  
  60. • dataflow formula
  61. The formula for the sum of all integers between 1 and n is n(n+1)÷2.  This method asks you for a value of n and computes the answer using the +,* and ÷ primitives.
  62.  
  63. • evaluate formula
  64. This method uses one "Evaluate operation" to calculate the above formula, instead of the three separate math operations used in the dataflow example.  An "Evaluate operation", if it can be used, is more efficient than the dataflow method.
  65.  
  66. • random (not directly executable)
  67. The "rand" primitive returns a random integer between 1 and 2,147,483,647 (1 and 2^31 - 1).  This method uses the rand primitive and the idiv(integer division) primitive to return a random number between 1 and any given number.
  68.  
  69. • Example Random
  70. Simply calls the random method described above to simulate rolling a die and getting a number between 1 and 6.
  71.  
  72.  
  73.  
  74. CLASS: System Primitives
  75. Prograph's "System Primitives" are sometimes called "Reflexive Primitives" because they allow you to programatically query your own code file.  For example, the "classes" primitive returns a list of all the classes in your file.  You can easily use these primitives to write a 
  76. self-documenter or browser for your source code.  The "Instance Finder" example in the Utilities file also makes use of these primitives.
  77.  
  78. • get a class
  79. Uses the classes primitive and the select primitive to display a list of all the classes in the file and return the one that the user selects.
  80.  
  81. • show class comments
  82. Calls "get a class", described above, and then uses the class-com primitive to show the comment on the chosen class.
  83.  
  84. • get a method
  85. Uses the "get a class" method described above, the methods primitive and the select primitive to display a list of all the methods of a particular class (or all the universal methods), and outputs both the class name and the method name the user selected.
  86.  
  87. • show method comments
  88. Calls "get a method", described above, and then uses the method-com primitive to show the comment on the chosen method.
  89.  
  90. • execute a method
  91. Calls "get a method", described above, and then uses an "Inject" terminal to call the selected method in the selected class.
  92.  
  93.  
  94. CLASS: String Primitives
  95. Most of these methods are trivial examples that let you get the hang of the string primitives ("length", "format", "in", "from-string", etc).  The Word and Char methods are more real-life types of examples.
  96.  
  97. • "length" example
  98. Illustration of the "length" primitive.
  99.  
  100. • "join" example
  101. Illustration of the "join" primitive. 
  102.  
  103. • "in" example
  104. Uses the "in" primitive to determine if a letter is a vowel or not.
  105.  
  106. • format example #1
  107. Use of the format primitive to achieve the same functionality as the "join" example.
  108.  
  109. • format example #2
  110. A little more complicated use of the format primitive to display a column of numbers with the decimals lined up.
  111.  
  112. • prefix example
  113. Illustration of the prefix primitive. 
  114.  
  115. • suffix example
  116. Illustration of the suffix primitive. 
  117.  
  118. • middle example
  119. Illustration of the middle primitive.
  120.  
  121. • to-ascii example
  122. Illustration of the to-ascii primitive.
  123.  
  124. • from-ascii example
  125. Illustration of the from-ascii primitive.
  126.  
  127. • to-string/from-string
  128. Uses the to-string and from-string primitives to determine the last digit of a given number.
  129.  
  130. • Char (not directly executable)
  131. Analogous to the "character" chunking function in Hypercard, returns the nth character of a string.
  132.  
  133. • Char example
  134. Calls the Char method described above
  135.  
  136. • Word (not directly executable)
  137. Analogous to the "word" chunking function in Hypercard, returns the nth word of a string.  Words are delimited by one or more spaces.
  138.  
  139. • skip spaces (not directly executable)
  140. Given a string and an index, returns the index of the next non-space character.  Called by the Word method described above.
  141.  
  142. • Word example
  143. Calls the Word method described above.
  144.  
  145. • Line (not directly executable)
  146. Analogous to the "line" chunking function in Hypercard, returns the nth line of a string.  Lines are delimited by carriage returns (ascii 0xD).
  147.  
  148. • Line example
  149. Calls the Line method described above.
  150.  
  151.  
  152.  
  153. CLASS: List Primitives
  154. Some of these methods are just trivial examples to help you get the hang of manipulating lists.  The cardinal/ordinal examples and shuffle and sort are more complex, but worth the effort to understand.
  155.  
  156. • attach-r example
  157. Illustration of the attach-r primitive.
  158.  
  159. • detach-r example
  160. Illustration of the detach-r primitive.
  161.  
  162. • attach-l example
  163. Illustration of the attach-l primitive.
  164.  
  165. • detach-nth example
  166. Illustration of the detach-nth primitive.
  167.  
  168. • get-nth example
  169. Illustration of the get-nth primitive.
  170.  
  171. • (length) example
  172. Illustration of the (length) primitive.
  173.  
  174. • (join) example
  175. Illustration of the (join) primitive.
  176.  
  177. • (in) example
  178. Illustration of the (in) primitive.
  179.  
  180. • list annotation example #1
  181. Illustrates how list annotations on roots and terminals work.  List annotations are one of the most powerful and useful features of the Prograph language.  They are well worth becoming comfortable with!
  182.  
  183. • list annotation example #2
  184. Illustrates what happens when you list annotate an input to an operation, in this case the show primitive.
  185.  
  186. • list annotation example #3
  187. Illustrates how you can use a list annotation on the output of a looping operation to collect individual results of each iteration into a list.
  188.  
  189. • get-cardinal-str (not directly executable)
  190. Given a number (such as 5), returns corresponding string (such as "five").  Uses the find-instance primitive to look up the number in a list of instances stored in the number xref class. 
  191.  
  192. • get-ordinal-str (not directly executable)
  193. Given a number in string or number form (such as 5 or "five"), returns corresponding ordinal number (such as "fifth").  Uses the find-instance primitive to look up the association in the number xref class.
  194.  
  195. • cardinal-ordinal example
  196. Uses list annotations in an interesting way, calling the get-cardinal-str and get-ordinal-str methods described above.
  197.  
  198. • palindrome? (not directly executable)
  199. Given a string, uses the to-ascii and reverse primitives to determine if the string is a palindrome (same backward and forward).
  200.  
  201. • palindrome example
  202. Calls the palindrome? method described above.
  203.  
  204. • shuffle (not directly executable)
  205. Takes a list as input, and creates a new list with the items randomly inserted.  Uses the (length), detach-nth and attach-r primitives, as well as the random method from the Math class in this file.
  206.  
  207. • shuffle example
  208. Called the shuffle method described above.
  209.  
  210. • sort (not directly executable)
  211. Implementation of a classic sort algorithm known as "quicksort".  Rather sophisticated use of list features of Prograph.  Uses a partition annotation, a list annotation, the primitives detach-l, attach-l and (join), and a concept known as recursion wherein a method calls itself.
  212.  
  213. • sort example
  214. Calls the sort method described above.
  215.  
  216.  
  217.  
  218. CLASS : I/O Primitives
  219. The i/o primitives allow you to easily post simple dialogs to get input from your user or show information.  The examples in this class are extremely simple and just give you an overview of the tools available to you.
  220.  
  221. • show example
  222. Illustrates the show primitive.
  223.  
  224. • ask example
  225. Illustrates the ask primitive.
  226.  
  227. • display example
  228. Illustrates the display primitive.
  229.  
  230. • accept example
  231. Illustrates the accept primitive.
  232.  
  233. • answer example
  234. Illustrates the answer primitive.
  235.  
  236. • answer-v example
  237. Illustrates the answer-v primitive.
  238.  
  239. • select example
  240. Illustrates the select primitive.
  241.  
  242. • dialog fonts
  243. Illustrates the set-dialog-font primitive.
  244.  
  245.  
  246. How to Use in Your Program
  247. --------------------------
  248.     Selectively load any method that seems useful.
  249.  
  250.