Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System.Collections.Generic
- let memoize (f : 'a -> 'b) =
- let cache = new Dictionary<'a, 'b>()
- let memoizedF x =
- match cache.TryGetValue(x) with
- | true, y -> y
- | false, _ ->
- printfn "computing..."
- let y = f x
- cache.Add(x, y)
- y
- memoizedF
- let rec y f x = f (y f) x
- let inc1 =
- let f = memoize (fun x -> x + 1)
- fun x -> f x
- let add2 =
- let f = memoize (fun (x, y) -> x + y)
- fun x y -> f(x, y)
- let mul3 : 'a -> 'a -> 'a -> 'a =
- let f = memoize (fun (x, y, z) -> x * y * z)
- fun x y z -> f(x, y, z)
Advertisement
Add Comment
Please, Sign In to add comment