Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

davidgrenier commented Sep 19, 2013

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

@ssboisen

This comment has been minimized.

Copy link
Owner Author

ssboisen commented Sep 20, 2013

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
You can’t perform that action at this time.