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

  • SEARCH
  • Home
  • 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 1002235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:48:09+00:00 2026-05-16T07:48:09+00:00

Is it possible to combine memoization and tail-recursion somehow? I’m learning F# at the

  • 0

Is it possible to combine memoization and tail-recursion somehow? I’m learning F# at the moment and understand both concepts but can’t seem to combine them.

Suppose I have the following memoize function (from Real-World Functional Programming):

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

and the following factorial function:

let rec factorial(x) = if (x = 0) then 1 else x * factorial(x - 1)

Memoizing factorial isn’t too difficult and making it tail-recursive isn’t either:

let rec memoizedFactorial =
  memoize (fun x -> if (x = 0) then 1 else x * memoizedFactorial(x - 1))

let tailRecursiveFactorial(x) =
  let rec factorialUtil(x, res) = if (x = 0)
                                  then res
                                  else let newRes = x * res
                                       factorialUtil(x - 1, newRes)
  factorialUtil(x, 1)

But can you combine memoization and tail-recursion? I made some attempts but can’t seem to get it working. Or is this simply not possible?

  • 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-05-16T07:48:10+00:00Added an answer on May 16, 2026 at 7:48 am

    As always, continuations yield an elegant tailcall solution:

    open System.Collections.Generic 
    
    let cache = Dictionary<_,_>()  // TODO move inside 
    let memoizedTRFactorial =
        let rec fac n k =  // must make tailcalls to k
            match cache.TryGetValue(n) with
            | true, r -> k r
            | _ -> 
                if n=0 then
                    k 1
                else
                    fac (n-1) (fun r1 ->
                        printfn "multiplying by %d" n  //***
                        let r = r1 * n
                        cache.Add(n,r)
                        k r)
        fun n -> fac n id
    
    printfn "---"
    let r = memoizedTRFactorial 4
    printfn "%d" r
    for KeyValue(k,v) in cache do
        printfn "%d: %d" k v
    
    printfn "---"
    let r2 = memoizedTRFactorial 5
    printfn "%d" r2
    
    printfn "---"
    
    // comment out *** line, then run this
    //let r3 = memoizedTRFactorial 100000
    //printfn "%d" r3
    

    There are two kinds of tests. First, this demos that calling F(4) caches F(4), F(3), F(2), F(1) as you would like.

    Then, comment out the *** printf and uncomment the final test (and compile in Release mode) to show that it does not StackOverflow (it uses tailcalls correctly).

    Perhaps I’ll generalize out ‘memoize’ and demonstrate it on ‘fib’ next…

    EDIT

    Ok, here’s the next step, I think, decoupling memoization from factorial:

    open System.Collections.Generic 
    
    let cache = Dictionary<_,_>()  // TODO move inside 
    let memoize fGuts n =
        let rec newFunc n k =  // must make tailcalls to k
            match cache.TryGetValue(n) with
            | true, r -> k r
            | _ -> 
                fGuts n (fun r ->
                            cache.Add(n,r)
                            k r) newFunc
        newFunc n id 
    let TRFactorialGuts n k memoGuts =
        if n=0 then
            k 1
        else
            memoGuts (n-1) (fun r1 ->
                printfn "multiplying by %d" n  //***
                let r = r1 * n
                k r) 
    
    let memoizedTRFactorial = memoize TRFactorialGuts 
    
    printfn "---"
    let r = memoizedTRFactorial 4
    printfn "%d" r
    for KeyValue(k,v) in cache do
        printfn "%d: %d" k v
    
    printfn "---"
    let r2 = memoizedTRFactorial 5
    printfn "%d" r2
    
    printfn "---"
    
    // comment out *** line, then run this
    //let r3 = memoizedTRFactorial 100000
    //printfn "%d" r3
    

    EDIT

    Ok, here’s a fully generalized version that seems to work.

    open System.Collections.Generic 
    
    let memoize fGuts =
        let cache = Dictionary<_,_>()
        let rec newFunc n k =  // must make tailcalls to k
            match cache.TryGetValue(n) with
            | true, r -> k r
            | _ -> 
                fGuts n (fun r ->
                            cache.Add(n,r)
                            k r) newFunc
        cache, (fun n -> newFunc n id)
    let TRFactorialGuts n k memoGuts =
        if n=0 then
            k 1
        else
            memoGuts (n-1) (fun r1 ->
                printfn "multiplying by %d" n  //***
                let r = r1 * n
                k r) 
    
    let facCache,memoizedTRFactorial = memoize TRFactorialGuts 
    
    printfn "---"
    let r = memoizedTRFactorial 4
    printfn "%d" r
    for KeyValue(k,v) in facCache do
        printfn "%d: %d" k v
    
    printfn "---"
    let r2 = memoizedTRFactorial 5
    printfn "%d" r2
    
    printfn "---"
    
    // comment out *** line, then run this
    //let r3 = memoizedTRFactorial 100000
    //printfn "%d" r3
    
    let TRFibGuts n k memoGuts =
        if n=0 || n=1 then
            k 1
        else
            memoGuts (n-1) (fun r1 ->
                memoGuts (n-2) (fun r2 ->
                    printfn "adding %d+%d" r1 r2 //%%%
                    let r = r1+r2
                    k r)) 
    let fibCache, memoizedTRFib = memoize TRFibGuts 
    printfn "---"
    let r5 = memoizedTRFib 4
    printfn "%d" r5
    for KeyValue(k,v) in fibCache do
        printfn "%d: %d" k v
    
    printfn "---"
    let r6 = memoizedTRFib 5
    printfn "%d" r6
    
    printfn "---"
    
    // comment out %%% line, then run this
    //let r7 = memoizedTRFib 100000
    //printfn "%d" r7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know you can combine multiple table-selects using a Join statement but is there
I have two onSelect functions that are both working individually, but is it possible
Is is possible to combine both the bar and line chart into a single
Is it possible to combine two wav files into one as if they've been
Is it possible to combine registers in vim? For example, if I have registers
Is it possible to combine a slideshow and show / hide div functionality? My
Is it possible to combine Ajax post with jQuery UI dialog? And I want
Also is it possible to combine this with removing periods from within the string?
Is it possible to combine Underscore's filter and map? I currently have two separate
Is it possible to combine ARC and non-ARC projects? I haven't really tried it

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.