Advertisement
Guest User

Untitled

a guest
Sep 19th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let rec sumListBy selector xs =
  2.     match xs with
  3.     | x::xs -> selector x + sumListBy selector xs
  4.     | [] -> 0
  5.  
  6. let rec filterListImpl predicate xs ys =
  7.     match xs with
  8.     | x::xs when predicate x -> filterListImpl predicate xs (x::ys)
  9.     | _::xs -> filterListImpl predicate xs ys
  10.     | [] -> ys
  11.  
  12. let filterList predicate xs = filterListImpl predicate xs []
  13.  
  14. let rec countChangeImpl (money: int) (coins: int list) (highestCoinUsed: int) =
  15.     match money with
  16.     | 0 -> 1
  17.     | money when money < 0 -> 0
  18.     | money ->
  19.         let usableCoins = filterList (fun coin -> coin >= highestCoinUsed) coins
  20.         sumListBy (fun coin -> countChangeImpl (money - coin) coins (max coin highestCoinUsed)) usableCoins
  21.  
  22. let countChange money coins = countChangeImpl money coins 0
  23.  
  24. let countChangeTest money coins expected =
  25.     let actual = countChange money coins
  26.     printf "money = %d; coins = %A; expected %d; actual %d\n" money coins expected actual
  27.  
  28. [<EntryPoint>]
  29. let main argv =
  30.     countChangeTest 301 [5;10;20;50;100;200;500] 0
  31.     countChangeTest 300 [500;5;50;100;20;200;10] 1022
  32.     countChangeTest 30 [10;20;100;50;5] 6
  33.     countChangeTest 4 [1;2] 3
  34.     System.Console.ReadLine() |> ignore
  35.     0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement