-
-
Save ssboisen/6622528 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
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 cannot do a when guard when both patterns do not bind the same variables.