home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Clean 1.2.4 / Small Demos / pascal.icl < prev    next >
Encoding:
Text File  |  1995-03-13  |  1.7 KB  |  78 lines  |  [TEXT/3PRM]

  1. module pascal
  2.  
  3. /*
  4. The Triangle of Pascal.
  5.  
  6. The result of this program is a real triangle of Pascal of height
  7. Height, not just a list representing such a triangle:
  8.  
  9.         1
  10.        1 1
  11.       1 2 1
  12.      1 3 3 1
  13.     1 4 6 4 1    etc.
  14.     
  15. Run the program using the "Basic Values Only" option (Application options).
  16. Use a non-proportional font for the output (e.g. Monaco 9).
  17. */
  18.  
  19. import StdEnv
  20.  
  21. /*    A row of the triangle is represented by a list of integers,
  22.     the triangle as a list of rows:
  23. */
  24.  
  25. ::Row        :== [Int]
  26. ::Triangle    :== [Row]
  27.  
  28. //    Some constants
  29.  
  30. NrRows :== 18    //    Number of rows to be shown.
  31. Middle :== 40    //    The middle of a 80 character line.
  32.  
  33. //    Miscellaneous functions
  34.  
  35. NrOfDigits:: Int -> Int
  36. NrOfDigits 0    =  0
  37. NrOfDigits n    =  NrOfDigits (n / 10) + 1
  38.  
  39. //    Calculating the Triangle.
  40.  
  41. Pascal::Triangle
  42. Pascal = p 
  43. where 
  44.     p = [[1] : [Next a \\ a <- p]]
  45.  
  46.     Next x =  AddRows [0:x] x
  47.  
  48.     AddRows::Row Row -> Row
  49.     AddRows [a:x] [b:y] =  [a + b : AddRows x y]
  50.     AddRows [a]   []    =  [a]
  51.     AddRows []    []    =  []
  52.  
  53. //    Formatting the list representing the triangle as a real triangle.
  54.  
  55. FormatRows::Triangle -> [String]
  56. FormatRows [f:r] =  [ FormatRow f  +++ "\n" : FormatRows r]
  57. where
  58.     FormatRow::Row -> String
  59.     FormatRow row
  60.         =   toString (spaces (Middle - Length_new row/2 ))  +++  FormatElems row 
  61.  
  62.     FormatElems::Row -> String
  63.     FormatElems [f:r] =  " " +++ toString f +++  FormatElems r  
  64.     FormatElems []    =  ""
  65.  
  66.     Length_new::Row -> Int
  67.     Length_new [f:r] =  NrOfDigits f +  Length_new r + 1
  68.     Length_new []     =  -1
  69. FormatRows []    =  []
  70.  
  71. /*    The Start rule: The first NrRows rows of the (infinite) triangle
  72.     returned by Pascal are taken and shown on the screen as a
  73.     triangle by means of FormatRows.
  74. */
  75.  
  76. Start::[String]
  77. Start = FormatRows (take NrRows Pascal)
  78.