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) |
This comment has been minimized.
This comment has been minimized.
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
This comment has been minimized.
You cannot do a when guard when both patterns do not bind the same variables.