OK, so we were learning about greedy algorithms at school and I implemented a very naive implementation of a change making algorithm for Canadian coins. Here's the Haskell code:
makeChange :: Int -> [Int]
makeChange amount = loop 0 [200, 100, 25, 10, 5, 1] []
where loop total coins@(c:cs) solution
| total == amount = solution
| null coins = error "no solution"
| otherwise = if total + c > amount then
loop total cs solution
else
loop (total + c) coins (c : solution)
(I could make this a lot better by returning an [(Int, Int)] and by using integer division, but I wanted to just follow the algorithm described in the textbook.)
To make sure that my code was correct, I wrote a QuickCheck property:
quickCheck (\(Positive n) -> sum (makeChange n) == n)
However, running this after compiling my file with GHC causes a stack overflow and I need to Ctrl+C out of the process.
On the other hand, the exact same algorithm in OCaml runs extremely quick and without a hicup.