home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / DDJ1289.ZIP / FISCHER.LST < prev    next >
File List  |  1989-10-30  |  2KB  |  75 lines

  1. _FUNCTIONAL PROGRAMMING AND FPCA'89_
  2. by Ronald Fischer
  3.  
  4. [FIGURE 1]
  5.  
  6.  
  7. DEC silly: LIST(CHAR)->LIST(LIST(CHAR));
  8. --- silly(NIL) <= ["Thank you for using <silly>"];
  9. --- silly(onechar::remaining) <=
  10.        (IF member(onechar,"0123456789") THEN "That's fine!\n"
  11.        ELSE "Please type only digits!\n") :: silly(remaining);
  12.  
  13. TYPEVAR any_type;
  14. DEC member: any_type # LIST(any_type) -> TRUVAL;
  15. --- member(_,NIL) <= FALSE;
  16. --- member(a,a::t) <= TRUE;
  17. --- member(a,_::t) <= member(a,t);
  18.  
  19.  
  20.  
  21. [FIGURE 2] 
  22.  
  23. || MIRANDA example program
  24. || defining a tree whose nodes can be any type
  25. || the type is represented by the asterisk symbol
  26. tree * ::= niltree | node * (tree *) (tree *)
  27.  
  28. || Now implementing a sort algorithm
  29. || To sort a tree, first flatten it to get a list of nodes
  30. || Next build a sorted tree from the list
  31. sort = flatten . buildsorted
  32.  
  33. || This defines how to flatten a tree
  34. flatten niltree = [] || Flattening an empty tree gives the empty list
  35. || To flatten any other tree, flatten the left subtree first, 
  36. || append the root, and append the flattened right subtree.
  37. flatten (node a left right) = flatten left ++ [a] ++ flatten right
  38.  
  39. || This defines how to build a sorted tree from an unsorted list
  40. || Note: "<=" and ">" must be defined on tree nodes!
  41. buildsorted = foldr insert niltree || foldr is a predefined transformer!
  42.        where
  43.        insert a niltree = node a niltree niltree
  44.        insert a (node b left right) 
  45.            = node b (insert a left) right, a<=b
  46.            = node b left (insert a right), a>b
  47.  
  48.  
  49. [FIGURE 3]
  50.  
  51. # Defining the FACTORIAL function in NIAL
  52. # i.e. factorial 4  results in 16
  53.  
  54. factorial IS FORK
  55.   [ 1 >= , 1 first , 
  56.     times [ pass , factorial (1 CONVERSE minus) ]
  57.   ]
  58.  
  59. # Defining the AVERAGE of a list of values
  60. # i.e. average 3 9 5 3  results in 4
  61. average IS / [sum,tally]
  62.  
  63.  
  64.  
  65. [FIGURE 4] 
  66.  
  67. main resps =
  68.   [ ReadFile "FOO" Text,
  69.     case resps of
  70.       (Return Val: _) ->
  71.           AppendChan "stdout" Text Val ]
  72.  
  73.     
  74.  
  75.