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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:55:17+00:00 2026-05-24T02:55:17+00:00

It hurts to ask it here. It really does. Every time I search in

  • 0

It hurts to ask it here. It really does. Every time I search in vain for the answers to my troubles, I see it. Taunting me. Stack Overflow.

Anyway, some hellish influence caused me to attempt to solve the Towers of Hanoi. My first solution was incomplete, as it resulted in a memory error if run with too many disks:

(define hanoi
  (lambda (n from to other)
    (cond ((< n 0)
       (error `(Error! Number of disks ,n cannot be less than 0!)))
      ((= n 0)
       '())
      (else
       (append (hanoi (- n 1)
              from
              other
              to)
           `((,from ,to))
           (hanoi (- n 1)
              other
              to
              from))))))

I read somewhere that continuation-passing style would solve the problem. However, this didn’t help either:

(define hanoi_cps
  (lambda (n from to other c)
    (cond ((< n 0)
       (error `(Error! Number of disks ,n cannot be less than 0!)))
      ((= n 0)
       (c '()))
      (else
       (hanoi_cps (- n 1)
              from
              other
              to
              (lambda (x)
            ((lambda (w)
               (w `((,from ,to))))
             (lambda (y)
               (hanoi_cps (- n 1)
                      other
                      to
                      from
                      (lambda (z)
                    (c (append x y z))))))))))))
  • 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-24T02:55:17+00:00Added an answer on May 24, 2026 at 2:55 am

    In continuation passing style, rather than extending the stack-space with recursive calls, you’re building up recursively-defined lambdas in the environment that your continuations are executed in … in other words, memory is used up somewhere along the line. For instance, with a simple factorial algorithm, you would normally write it something like:

    (define (factorial x)
        (cond ((eq? x 0) 1))
              ((eq? x 1) 1))
              (else (* x (factorial (- x 1))))))
    

    With this recursive definition for factorial, stack space is going to be used up to hold the arguments to the deferred multiply operation peformed in each recursive function call. A continuation-passing version of the same function would look like:

    (define (factorial x cont)
        (cond ((eq? x 0) (cont 1))
              ((eq? x 1) (cont 1))
              (else (factorial (- x 1) (lambda (y) (cont (* x y)))))))
    

    What would have consumed stack-space before is now used up by the environment of the anonymous lambda. The environment of the lambda in this case is being filled with values that are required in order to resolve the value of x and cont with each recursive call to factorial. Since cont itself is a lambda with an environment, you can see how memory will eventually be consumed as each lambda-continuation will need to store in its environment the lambda from the previous call to factorial … this creates a recursively defined lambda-continuation that has an environment that is basically a recursive list of all the continuations that have been accrued though the recursive calls to factorial.

    One way of looking at continuation-passing style is that while you’ve basically converted the function-calling mechanism to a tail-recursive method, the actual definitions of the continuations themselves are recursive in nature, so you’re not really removing the recursive-nature of the algorithm per-se … in other words evaluating a continuation built up over tail-recursive calls requires evaluating a recursively defined continuation inside of it, which itself has another recursively defined continuation inside of it, etc. The environment for the lambda-continuations ends up looking like a list-of-a-list-of-a-list, etc. Storing all those recursive definitions in the environment of the lambda-continuation requires memory, so whether you’re consuming space on the stack via a normal recursive calling convention, or consuming memory space by storing recursively defined environments in every lambda-continuation, either way you’re eventually going to run out of space.

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

Sidebar

Related Questions

OK guys... I'm missing something so basic here that it hurts to even ask.
Since I don't really have a good idea for word to search with myself
My brain is starting to hurt so I decided I'd ask here. I have
I don't think this is possible, but it never hurts to ask. Is there
Every first time your app is sending local notification to lock screen you can
i have tried till my head hurts and have been reading so much my
I am rather confused by the hurt-mongering here . I know how to do
Alright guys, I really hurt my brain over this one and I'm curious if
Okay this is probably a really dumb question, however it's really starting to hurt.
my head hurts today! :) I need some help with rails deployment. I migrated

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.