home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / makechange < prev    next >
Text File  |  2020-01-01  |  1KB  |  40 lines

  1. ; makechange
  2. ; Dat Thuc Nguyen
  3. ; 22 November 2003
  4. ;
  5. ; How many different ways can we make change of $1.00, given half-dollars,
  6. ; quarters, dimes, nickels, and pennies?  The solution in Scheme is given
  7. ; in the book Structure and Interpretation Of Computer programs by Harold
  8. ; Abelson.  Below is the solution in C-Kermit scripting language. It's not
  9. ; about counting change only, but it's about C-Kermit scripting language's
  10. ; ability to solve a wide variety of computational problems.  The key
  11. ; is you don't need Delphi or C++ at all while using C-Kermit.
  12.  
  13. define count_change {
  14. ; \%1 amount to be changed
  15.        (cc \%1 5)
  16. }
  17.  
  18. define cc {
  19. ; \%1 amount to be changed
  20. ; \%2 kinds-of-coins
  21.       (if (== \%1 0) 1
  22.           (if (OR (< \%1 0) (== \%2 0)) 0
  23.               (+ (cc \%1  (- \%2 1))
  24.                  (cc (- \%1
  25.                         (first_denomination \%2))
  26.                      \%2))))
  27. }
  28.  
  29. define first_denomination {
  30. ; \%1 kinds-of-coins
  31.        (if (== \%1 1) 1
  32.            (if (== \%1 2) 5
  33.                (if (== \%1 3) 10
  34.                    (if (== \%1 4) 25
  35.                        (if (== \%1 5) 50)))))
  36. }
  37.  
  38. (count_change 100)                      ; for one dollar
  39. (count_change   50)                     ; for half-dollar
  40.