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

The Archive Base Latest Questions

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

For some reason, I am having trouble thinking of a good way to rewrite

  • 0

For some reason, I am having trouble thinking of a good way to rewrite this function so it uses constant stack space. Most online discussions of tree recursion cheat by using the Fibonacci function and exploiting the properties of that particular problem. Does anyone have any ideas for this ‘real-world’ (well, more real-world than the Fibonacci series) use of recursion?

Clojure is an interesting case since it does not have tail-call optimization, but only tail recursion via the ‘recur’ special form. It also strongly discourages the use of mutable state. It does have many lazy constructs including tree-seq, but I am not able to see how they can help me for this case. Can anyone share some techniques they have picked up from C, Scheme, Haskell, or other programming languages?

(defn flatten [x]   (let [type (:type x)]     (cond (or (= type :NIL) (= type :TEXT))           x           (= type :CONCAT)           (doc-concat (flatten (:doc1 x))                       (flatten (:doc2 x)))           (= type :NEST)           (doc-nest (:level x)                     (flatten (:doc x)))           (= type :LINE)           (doc-text ' ')           (= type :UNION)           (recur (:doc1 x))))) 

edit: By request in the comments…

Restated in general terms and using Scheme — how do I rewrite the following recursion pattern so it doesn’t consume stack space or require tail-call optimization of non-self-calls?

(define (frob x)   (cond ((foo? x)          x)         ((bar? x)          (macerate (f x) (frob (g x))))         ((thud? x)          (frobnicate (frob (g x))                      (frob (h x)))))) 

I chose annoying names to drive home the point that I am looking for answers that don’t rely on the algebraic properties of x, macerate, frobnicate, f, g, or h. I just want to rewrite the recursion.

UPDATE:

Rich Hickey has kindly added an explicit trampoline function to Clojure.

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

    This may look ugly but it’s a way to do it in trampoline-style (no system stack overflow), and without using gotos.

    push x,1 on homemade stack while stack length > 1   n = pop   if (n==1)     x = pop     if (type(x)==NIL || type(x)==TEXT)       push x // this is the "return value"     else if (type(x)==CONCAT)       push 2 // say call doc-concat       push doc2(x), 1 // 2nd recursion       push doc1(x), 1 // 1st recursion     else if (type(x)==NEST)       push 3 // say call doc-nest       push level(x) // push level argument to doc-nest       push doc(x), 1 // schedule recursion     else if (type(x)==LINE)       push " " // return a blank     else if (type(x)==UNION)       push doc1(x), 1 // just recur   else if (n==2)     push doc-concat(pop, pop) // finish the CONCAT case   else if (n==3)     push doc-nest(pop, pop) // finish the NEST case   endif endwhile // final value is the only value on the stack 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • 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 simply use the redistributable installer for mdx provided by microsoft.… May 11, 2026 at 10:14 am
  • added an answer Right now programmers think of source code control as integrating… May 11, 2026 at 10:14 am
  • added an answer Many users change the font size of your page by… May 11, 2026 at 10:14 am

Related Questions

For some reason, I am having trouble thinking of a good way to rewrite
For some reason I am having troubles with a DBI handle. Basically what happened
I am working on a little Perl module and for some reason I had
I am using VMWare tools for Ubuntu Hardy, but for some reason vmware-install.pl finds
I am trying something very simple, but for some reason it does not work.
Ok, I am reading in dat files into a byte array. For some reason,
For some reason, when I try to install SQL Server 2008 Express , I
For some reason, no matter how I go about it, I cannot get TortoiseSVN
For some reason, the event listener I define never seems to receive any events,
For some reason I never see this done. Is there a reason why not?

Trending Tags

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

Top Members

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.