Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8349947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:09:51+00:00 2026-06-09T08:09:51+00:00

I’m trying to memoize a member function of a class, but every time the

  • 0

I’m trying to memoize a member function of a class, but every time the member is called (by another member) it makes a whole new cache and ‘memoized’ function.

member x.internal_dec_rates = 
        let cache = new Dictionary< Basis*(DateTime option), float*float>()
        fun (basis:Basis) (tl:DateTime option) ->
            match cache.TryGetValue((basis,tl)) with
            | true, (sgl_mux, sgl_lps) -> (sgl_mux, sgl_lps)
            | _ ->
                let (sgl_mux, sgl_lps) =
                    (* Bunch of stuff *)
                cache.Add((basis,tl),(sgl_mux,sgl_lps))
                sgl_mux,sgl_lps

I’m using Listing 10.5 in “Real World Functional Programming” as a model. I’ve tried using a memoization higher-order function and that doesn’t help. The above listing has the memoization built in directly.

The problem is, when I call it e.g.

member x.px (basis:Basis) (tl: DateTime option) = 
        let (q,l) = (x.internal_dec_rates basis tl)
        let (q2,l2) = (x.internal_dec_rates basis tl)
        (exp -q)*(1.-l)

execution goes to the ‘let cache=…’ line, defeating the whole point. I put in the (q2,l2) line in order to make sure it wasn’t a scope problem, but it doesn’t seem to be.

In fact I did a test using Petricek’s code as a member function and that seems to have the same issue:

// Not a member function
let memo1 f =
    let cache = new Dictionary<_,_>()
    (fun x ->
        match cache.TryGetValue(x) with
        | true, v -> v
        | _ -> let v = f x
               cache.Add(x,v)
               v
    )

member x.factorial = memo1(fun y->
    if (y<=0) then 1 else y*x.factorial(y-1))

Even the internal recursion of x.factorial seems to set up a new ‘cache’ for each level.

What am I doing wrong, and how can I make this work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:09:52+00:00Added an answer on June 9, 2026 at 8:09 am

    In response to your comment on Jack’s answer, this doesn’t have to become tedious. Given a memoize function:

    let memoize f =
      let cache = Dictionary()
      fun x ->
        match cache.TryGetValue(x) with
        | true, v -> v
        | _ -> 
          let v = f x
          cache.Add(x, v)
          v
    

    Define each of your functions as let-bound values and return them from your methods:

    type T() as x =
      let internalDecRates = memoize <| fun (basis: Basis, tl: DateTime option) ->
        (* compute result *)
        Unchecked.defaultof<float * float>
    
      let px = memoize <| fun (basis, tl) ->
        let (q,l) = x.InternalDecRates(basis, tl)
        let (q2,l2) = x.InternalDecRates(basis, tl)
        (exp -q)*(1.-l)
    
      member x.InternalDecRates = internalDecRates
      member x.Px = px
    

    The only “boilerplate” is the let binding and call to memoize.

    EDIT: As kvb noted, in F# 3.0 auto-properties allow a more concise solution:

    type T() as x =
      member val InternalDecRates = memoize <| fun (basis: Basis, tl: DateTime option) ->
        (* compute result *)
        Unchecked.defaultof<float * float>
    
      member val Px = memoize <| fun (basis, tl) ->
        let (q,l) = x.InternalDecRates(basis, tl)
        let (q2,l2) = x.InternalDecRates(basis, tl)
        (exp -q)*(1.-l)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.