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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:31:34+00:00 2026-05-13T20:31:34+00:00

Exercise 1.11 : A function f is defined by the rule that f(n) =

  • 0

Exercise 1.11:

A function f is defined by the rule that f(n) = n if n < 3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n > 3. Write a procedure that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative process.

Implementing it recursively is simple enough. But I couldn’t figure out how to do it iteratively. I tried comparing with the Fibonacci example given, but I didn’t know how to use it as an analogy. So I gave up (shame on me) and Googled for an explanation, and I found this:

(define (f n)
   (if (< n 3)
       n
       (f-iter 2 1 0 n)))

(define (f-iter a b c count)
   (if (< count 3)
       a
       (f-iter (+ a (* 2 b) (* 3 c))
               a
               b
               (- count 1))))

After reading it, I understand the code and how it works. But what I don’t understand is the process needed to get from the recursive definition of the function to this. I don’t get how the code could have formed in someone’s head.

Could you explain the thought process needed to arrive at the solution?

  • 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-13T20:31:34+00:00Added an answer on May 13, 2026 at 8:31 pm

    You need to capture the state in some accumulators and update the state at each iteration.

    If you have experience in an imperative language, imagine writing a while loop and tracking information in variables during each iteration of the loop. What variables would you need? How would you update them? That’s exactly what you have to do to make an iterative (tail-recursive) set of calls in Scheme.

    In other words, it might help to start thinking of this as a while loop instead of a recursive definition. Eventually you’ll be fluent enough with recursive -> iterative transformations that you won’t need to extra help to get started.


    For this particular example, you have to look closely at the three function calls, because it’s not immediately clear how to represent them. However, here’s the likely thought process: (in Python pseudo-code to emphasise the imperativeness)

    Each recursive step keeps track of three things:

    f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) 
    

    So I need three pieces of state to track the current, the last and the penultimate values of f. (that is, f(n-1), f(n-2) and f(n-3).) Call them a, b, c. I have to update these pieces inside each loop:

    for _ in 2..n:
        a = NEWVALUE
        b = a
        c = b
    return a
    

    So what’s NEWVALUE? Well, now that we have representations of f(n-1), f(n-2) and f(n-3), it’s just the recursive equation:

    for _ in 2..n:
        a = a + 2 * b + 3 * c
        b = a
        c = b
    return a
    

    Now all that’s left is to figure out the initial values of a, b and c. But that’s easy, since we know that f(n) = n if n < 3.

    if n < 3: return n
    a = 2 # f(n-1) where n = 3
    b = 1 # f(n-2)
    c = 0 # f(n-3)
    # now start off counting at 3
    for _ in 3..n:
        a = a + 2 * b + 3 * c
        b = a
        c = b
    return a
    

    That’s still a little different from the Scheme iterative version, but I hope you can see the thought process now.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Only primitive values (strings, dates, booleans, numbers) and objects and… May 14, 2026 at 11:40 pm
  • Editorial Team
    Editorial Team added an answer If you're on the internet, you might be able to… May 14, 2026 at 11:40 pm
  • Editorial Team
    Editorial Team added an answer Use if (condition) { } else if (condition) { }… May 14, 2026 at 11:40 pm

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.