let memoize (f : 'a -> 'b) = let cache = new Dictionary<'a, 'b>() let memoizedF x = match cache.TryGetValue(x) with | true, y -> y | false, _ -> let y = f x cache.Add(x, y) y memoizedF