Skip to content

Instantly share code, notes, and snippets.

@ssboisen
Forked from bennage/count_change.fs
Last active December 23, 2015 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ssboisen/6622528 to your computer and use it in GitHub Desktop.
Save ssboisen/6622528 to your computer and use it in GitHub Desktop.
let memoize2 f =
let cache = ref Map.empty
fun a b ->
match (!cache).TryFind (a,b) with
| Some(cv) -> cv
| None ->
let v = f a b
cache := Map.add (a,b) v !cache
v
let rec slowcc amount coins =
match (amount, coins) with
| (0,_) -> 1
| (_,[]) -> 0
| (amount,_) when amount < 0 -> 0
| (_, hd :: tail) -> slowcc amount tail + slowcc (amount-hd) coins
let rec fastcc = memoize2 (fun amount coins ->
match (amount, coins) with
| (0,_) -> 1
| (_,[]) -> 0
| (amount,_) when amount < 0 -> 0
| (_, hd :: tail) -> fastcc amount tail + fastcc (amount-hd) coins)
@davidgrenier
Copy link

You cannot do a when guard when both patterns do not bind the same variables.

@ssboisen
Copy link
Author

That is true, that'll teach me to edit code without a compiler :-)

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