Skip to content

Instantly share code, notes, and snippets.

@bennage
Created September 19, 2013 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save bennage/6617800 to your computer and use it in GitHub Desktop.
Save bennage/6617800 to your computer and use it in GitHub Desktop.
An F# implementation of the counting change problem's solution, following the clojure example from this post: http://www.billthelizard.com/2010/12/sicp-219-counting-change-revisited.html Can you improve it?
open System;
open System.Diagnostics;
[<EntryPoint>]
let main argv =
let rec cc amount coins =
match (amount, coins) with
| (0,_) -> 1
| (_,[]) -> 0
| (amount,_) when amount < 0 -> 0
| _ -> cc amount coins.Tail + cc (amount-coins.Head) coins
let stopWatch = Stopwatch.StartNew()
let x = cc 100 [1;2;3;4;5;6;7]
stopWatch.Stop();
printfn "elapsed: %f" stopWatch.Elapsed.TotalMilliseconds
Console.ReadLine() |> ignore
0
@mausch
Copy link

mausch commented Sep 23, 2013

IMHO pattern matching doesn't make much sense here, and actually harm readability. I'd use ifs instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment