Juliet

Untitled

Oct 3rd, 2011
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.63 KB | None | 0 0
  1. open System.Collections.Generic
  2.  
  3. let memoize (f : 'a -> 'b) =
  4.     let cache = new Dictionary<'a, 'b>()
  5.    
  6.     let memoizedF x =
  7.         match cache.TryGetValue(x) with
  8.         | true, y -> y
  9.         | false, _ ->
  10.             printfn "computing..."
  11.             let y = f x
  12.             cache.Add(x, y)
  13.             y
  14.  
  15.     memoizedF
  16.  
  17. let rec y f x = f (y f) x
  18.  
  19. let inc1 =
  20.     let f = memoize (fun x -> x + 1)
  21.     fun x -> f x
  22.  
  23. let add2 =
  24.     let f = memoize (fun (x, y) -> x + y)
  25.     fun x y -> f(x, y)
  26.  
  27. let mul3 : 'a -> 'a -> 'a -> 'a =
  28.     let f = memoize (fun (x, y, z) -> x * y * z)
  29.     fun x y z -> f(x, y, z)
  30.  
Advertisement
Add Comment
Please, Sign In to add comment