I believe that what the blog post is trying to demonstrate is that the lazy value's Force member is memoized; forcing a single lazy value multiple times will only result in the lazy value's definition being executed a single time. I don't think that the blogger was trying to imply that applying "lazy" would memoize functions in general.

Having said that, I don't see what's so "cumbersome" about the traditional memoization pattern:

1
2
3
4
5
6
7
8
9
10
11
12
13
let memoize f =
  let dict = System.Collections.Generic.Dictionary<_,_>()
  fun x ->
    if not (dict.ContainsKey x) then
      dict.[x] <- f x
    dict.[x]


let f = 
  memoize (fun x ->
    let ret = "hi " + x
    printfn "calling f %s" x
    ret)
By on 12/10/2010 7:25 AM ()

Isn't that lazy before it is forced is just a function wrapper around a computation in the first place ?

I think in your case instead of returning lazy from the function you should have regular function with the computation and then

have something like: let susp = lazy (f 4)

so when you call susp.Force()

f 4 is evaluated once, and the result is memoized

By on 12/10/2010 5:24 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper