home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gofer230.zip / Progs / Gofer / Docs / ch05 < prev    next >
Text File  |  1994-06-23  |  4KB  |  133 lines

  1.  
  2.  
  3. Introduction to Gofer            5. STANDARD AND USER-DEFINED FUNCTIONS
  4.  
  5.  
  6. 5. STANDARD AND USER-DEFINED FUNCTIONS
  7.  
  8. The function "sum" used in the examples above, and indeed the  addition
  9. and multiplication functions (+) and (*), are  all  standard  functions
  10. which are included as part of a large collection  of  functions  called
  11. the `standard prelude' which are loaded into the Gofer system each time
  12. that you start the interpreter.  Quite a number of useful  calculations
  13. can be carried out using these functions alone, but  for  more  general
  14. use you can also define your own functions and store the definitions in
  15. a file so that these functions can be loaded and used by by  the  Gofer
  16. system.  For example, suppose that you create a file "fact"  containing
  17. the following definition:
  18.  
  19.      fact n = product [1..n]
  20.  
  21. The "product" function is another standard function which can  be  used
  22. to calculate the product of a list of integers, and so the  line  above
  23. defines a  function  "fact"  which  calculates  the  factorial  of  its
  24. argument.  In standard  mathematical notation,  fact n = n!   which  is
  25. usually defined informally by an equation of the form:
  26.  
  27.                  n! = 1 * 2 * ... * (n-1) * n
  28.  
  29. Once you become familiar with the notation used by Gofer, you will  see
  30. that the Gofer definition of the  factorial  function  is  really  very
  31. similar to this informal mathematical definition.
  32.  
  33. In order to use this definition from the  Gofer  interpreter,  we  must
  34. first load the definitions of  the  file  into  the  interpreter.   The
  35. simplest way to do this uses the ":l" command:
  36.  
  37.     ? :l fact
  38.     Reading script file "fact":
  39.     Parsing......................................................
  40.     Dependency analysis..........................................
  41.     Type checking................................................
  42.     Compiling....................................................
  43.  
  44.     Gofer session for:
  45.     /gofer/prelude
  46.     fact
  47.     ?
  48.  
  49. Notice the list of filenames displayed after "Gofer session for:"; this
  50. tells you which files of definitions are currently being used by Gofer,
  51. the first of which is the  file  containing  the  definitions  for  the
  52. standard prelude.  Since the file  containing  the  definition  of  the
  53. factorial function has now  been  loaded,  we  can  make  use  of  this
  54. function in expressions entered to the interpreter:
  55.  
  56.     ? fact 6
  57.     720
  58.     (57 reductions, 85 cells)
  59.  
  60. For another example, recall the  standard  mathematical  formula  which
  61. tells us that  the  number  of  ways  of  choosing  r  objects  from  a
  62.  
  63.  
  64.                                       6
  65.  
  66.  
  67.  
  68.  
  69. Introduction to Gofer            5. STANDARD AND USER-DEFINED FUNCTIONS
  70.  
  71.  
  72. collection of n objects is given by n! / (r! * (n-r)!).  In Gofer, this
  73. function can be defined by:
  74.  
  75.     comb n r = fact n /(fact r * fact (n-r))
  76.  
  77. In order to use this function, we can either edit the file "fact" which
  78. contains  the  definition  of  the  factorial  function,   adding   the
  79. definition of "comb" on a new line, or we can include the definition as
  80. part of an expression entered whilst using Gofer:
  81.  
  82.     ? comb 5 2 where comb n r = fact n /(fact r * fact (n-r))
  83.     10
  84.     (110 reductions, 161 cells)
  85.     ? 
  86.  
  87. The ability to define a function as part of an expression like this  is
  88. often quite useful.  However, if the function "comb" were likely to  be
  89. wanted on a number of occasions, it would be more sensible to  add  its
  90. definition to the contents of the file "fact",  instead  of  having  to
  91. repeat the definition each time it is used.
  92.  
  93. You will learn more about the functions defined in the standard prelude
  94. and find out  how  to  define  your  own  functions  in  the  following
  95. sections.
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.                                       7
  131.  
  132.  
  133.