home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 12 / CD_ASCQ_12_0294.iso / maj / 666 / h2.hlp < prev    next >
Text File  |  1992-12-04  |  2KB  |  60 lines

  1.  
  2.                        FUNCTION DEFINITION
  3.  
  4. The syntax of function definition in Proxy is the same as that in C
  5. with minor differences: (1) the declaration of local variables is different
  6. mainly due to the fact that the types of variables are not declared in
  7. Proxy and (2) each function declaration in Proxy is terminated with a
  8. semi-colon, (3) it is not necessary to name the top level function 'main',
  9. (4) procedures do not require the prefix 'void'; in what follows we will
  10. use the generic term function to apply as well to procedures. The syntax
  11. is now shown:
  12.  
  13. identifier ( op-param-list op-decl) block ;
  14.  
  15. The definition consists of the function name (identifier) followed by
  16. an optional parameter-list and an optional list of local variables
  17. delimited by parentheses. The parameter-list consists of identifiers
  18. separated by commas. The list of local variables consists of a semi-
  19. colon followed by identifiers separated by commas. The block consists
  20. of a statement-list (see the section on statements) delimited by
  21. curly brackets. Finally, the definition must be terminated with a
  22. semi-colon. Examples follow (the return statement is described in the
  23. section on statements).
  24.  
  25. even(n) {return n%2==0;};
  26. odd(n) {return !even(n);};
  27.  
  28. Given a subset of the integers, say digits={0..9};, we can define
  29. a function to obtain the odd integers from this set.
  30.  
  31. odddigits() {return {n:n<-digits;odd(n)};};
  32.  
  33. The following example illustrates a simple recursive function to
  34. add up the elements in an integer sequence.
  35.  
  36. reduce(s) { if(s==[]) return 0; else return hd s + reduce(tl s);};
  37.  
  38. The next example illustrates the definition of a doubly recursive
  39. function, and the declaration of a local variable x.
  40.  
  41. quicksort(s;x) { if(len s<2) return s;
  42.               x=s[1];
  43.               return quicksort([y:y<-s;y<x]) conc [x]
  44.                 conc quicksort([y:y<-tl s;y>=x]);};
  45.  
  46. An example showing the use of the existential quantifier:
  47.  
  48. primes(max) {return [n:n<-[2..max];
  49.              !(exists m in {2..n-1};(n % m)==0)];};
  50.  
  51.  
  52. An example of a procedure (a function which does not contain a return
  53. statement) which has no parameters.
  54.  
  55. init() {x["a"]="b";};
  56.  
  57. This procedure initializes the variable x to the map {"a"->"b"}. The
  58. same effect could have been achieved at the command level by the
  59. assignment x["a"]="b";.
  60.