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 7699701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:32:24+00:00 2026-05-31T22:32:24+00:00

In clojure, I would like to write a tail-recursive function that memoizes its intermediate

  • 0

In clojure, I would like to write a tail-recursive function that memoizes its intermediate results for subsequent calls.

[EDIT: this question has been rewritten using gcd as an example instead of factorial.]

The memoized gcd (greatest common divisor) could be implemented like this:

(def gcd (memoize (fn [a b] 
   (if (zero? b) 
       a 
       (recur b (mod a b)))) 

In this implementation, intermediate results are not memoized for subsequent calls. For example, in order to calculate gcd(9,6), gcd(6,3) is called as an intermediate result. However, gcd(6,3) is not stored in the cache of the memoized function because the recursion point of recur is the anonymous function that is not memoized.

Therefore, if after having called gcd(9,6), we call gcd(6,3) we won’t benefit from the memoization.

The only solution I can think about will be to use mundane recursion (explicitely call gcd instead of recur) but then we will not benefit from Tail Call Optimization.

Bottom Line

Is there a way to achieve both:

  1. Tail call optimization
  2. Memoization of intermediate results for subsequent calls

Remarks

  1. This question is similar to Combine memoization and tail-recursion. But all the answers there are related to F#. Here, I am looking for an answer in clojure.
  2. This question has been left as an exercise for the reader by The Joy of Clojure (chap 12.4). You can consult the relevant page of the book at http://bit.ly/HkQrio.
  • 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-31T22:32:25+00:00Added an answer on May 31, 2026 at 10:32 pm

    in your case it’s hard to show memoize do anything with factorial because the intermediate calls are unique, so I’ll rewrite a somewhat contrived example assuming the point is to explore ways to avoid blowing the stack:

    (defn stack-popper [n i] 
        (if (< i n) (* i (stack-popper n (inc i))) 1)) 
    

    which can then get something out of a memoize:

    (def stack-popper 
       (memoize (fn [n i] (if (< i n) (* i (stack-popper n (inc i))) 1))))
    

    the general approaches to not blowing the stack are:

    use tail calls

    (def stack-popper 
        (memoize (fn [n acc] (if (> n 1) (recur (dec n) (* acc (dec n))) acc))))
    

    use trampolines

    (def stack-popper 
        (memoize (fn [n acc] 
            (if (> n 1) #(stack-popper (dec n) (* acc (dec n))) acc))))
    (trampoline (stack-popper 4 1))
    

    use a lazy sequence

    (reduce * (range 1 4))
    

    None of these work all the time, though I have yet to hit a case where none of them work. I almost always go for the lazy ones first because I find them to be most clojure like, then I head for tail calling with recur or tramplines

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In clojure, I would like to write a defn-my macro that creates a function
Is there a pretty printing function in Clojure that would output data-structures like lists
I would like to write a function replace-several that receives a string and a
For a JQuery plugin that I am trying to write I would like to
I would like to write a simple multiplexing server in Clojure (as a sample
I would like to know to to write most efficient LINQ ( EDIT :
I'm not very familiar with Clojure/Lisp macros. I would like to write apply-recur macro
I would like to move data back and fourth between clojure applications. Application settings
What I would like to do (in Clojure): For example, I have a vector
Essentially, I want a function that works like this: user=> (pos 'c '(a b

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.