Advertisement
Guest User

Untitled

a guest
Jan 16th, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.09 KB | None | 0 0
  1. // Learn more about F# at http://fsharp.net
  2. // See the 'F# Tutorial' project for more help.
  3. open System.Collections.Generic
  4.  
  5. let inline  left (x, y) = (x + 1, y)
  6. let inline down (x, y) = (x, y + 1)
  7. let max = 30
  8. let (|Left|Down|Both|End|) (x, y) =
  9.                   if (x < max && y >= max) then Left
  10.                   else if (x >= max && y < max) then Down
  11.                   else if (x < max && y < max) then Both
  12.                   else End
  13.  
  14. let cache = Dictionary<_,_>()  
  15.  
  16. let addToCache x res =
  17.     cache.Add(x,res)
  18.     res
  19.  
  20. let rec compute2 x =
  21.     match cache.TryGetValue(x) with
  22.     | true,r -> r
  23.     | _ ->
  24.             match x with
  25.             | End ->  1L
  26.             | Left -> compute2 (left x) |> addToCache x
  27.             | Down -> compute2 (down x) |> addToCache x
  28.             | Both -> compute2 (left x) + compute2 (down x) |> addToCache x
  29.  
  30.  
  31. [<EntryPoint>]
  32. let main argv =
  33.   //  let processing = compute (1, 1) 0L (fun x -> x)
  34.     //printfn "%A" processing
  35.     let processing = compute2 (1, 1)
  36.     printfn "%A" processing
  37.     0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement