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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:23:08+00:00 2026-05-10T21:23:08+00:00

I’m a Lisp beginner. I’m trying to memoize a recursive function for calculating the

  • 0

I’m a Lisp beginner. I’m trying to memoize a recursive function for calculating the number of terms in a Collatz sequence (for problem 14 in Project Euler). My code as of yet is:

(defun collatz-steps (n)   (if (= 1 n) 0        (if (evenp n)             (1+ (collatz-steps (/ n 2)))            (1+ (collatz-steps (1+ (* 3 n)))))))  (defun p14 ()   (defvar m-collatz-steps (memoize #'collatz-steps))   (let        ((maxsteps (funcall m-collatz-steps 2))        (n 2)        (steps))     (loop for i from 1 to 1000000           do            (setq steps (funcall m-collatz-steps i))           (cond              ((> steps maxsteps)               (setq maxsteps steps)              (setq n i))             (t ())))     n))   (defun memoize (fn)   (let ((cache (make-hash-table :test #'equal)))     #'(lambda (&rest args)         (multiple-value-bind                (result exists)             (gethash args cache)           (if exists               result               (setf (gethash args cache)                     (apply fn args))))))) 

The memoize function is the same as the one given in the On Lisp book.

This code doesn’t actually give any speedup compared to the non-memoized version. I believe it’s due to the recursive calls calling the non-memoized version of the function, which sort of defeats the purpose. In that case, what is the correct way to do the memoization here? Is there any way to have all calls to the original function call the memoized version itself, removing the need for the special m-collatz-steps symbol?

EDIT: Corrected the code to have

(defvar m-collatz-steps (memoize #'collatz-steps)) 

which is what I had in my code. Before the edit I had erroneously put:

(defvar collatz-steps (memoize #'collatz-steps)) 

Seeing that error gave me another idea, and I tried using this last defvar itself and changing the recursive calls to

       (1+ (funcall collatz-steps (/ n 2)))        (1+ (funcall collatz-steps (1+ (* 3 n)))) 

This does seem to perform the memoization (speedup from about 60 seconds to 1.5 seconds), but requires changing the original function. Is there a cleaner solution which doesn’t involve changing the original function?

  • 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. 2026-05-10T21:23:09+00:00Added an answer on May 10, 2026 at 9:23 pm

    I assume you’re using Common-Lisp, which has separate namespaces for variable and function names. In order to memoize the function named by a symbol, you need to change its function binding, through the accessor `fdefinition’:

    (setf (fdefinition 'collatz-steps) (memoize #'collatz-steps))  (defun p14 ()   (let ((mx 0) (my 0))     (loop for x from 1 to 1000000           for y = (collatz-steps x)           when (< my y) do (setf my y mx x))     mx)) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 57k
  • Answers 57k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Managed to fix it through the ItemSelectionModel every view apparently… May 11, 2026 at 8:27 am
  • added an answer You can return a double, by accessing the m_dt member… May 11, 2026 at 8:27 am
  • added an answer Down for me too (java.sun.com seems to be MIA at… May 11, 2026 at 8:27 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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

MEILI PLUGIN ACTIVE

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.