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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:07:02+00:00 2026-06-12T14:07:02+00:00

I am trying to write two separate tail recursive functions that compute the length

  • 0

I am trying to write two separate tail recursive functions that compute the length of a list and I am given these limitations:

  1. write a version, lengtht that is tail recursive and uses external (non-nested) auxiliary functions as needed

  2. write a second version, lengtht2 that uses no additional top-level functions. The function should still be tail-recursive, and can use any local bindings that you want

I am new to racket and this is what I understand the general form of tail recursion is:

(define (func x)
    (cond (end-test-1 end-value-1)
          (end-test-2 end-value-2)
          (else (func reduced-x))))

I am just a little confused about how to do this

  • 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-06-12T14:07:03+00:00Added an answer on June 12, 2026 at 2:07 pm

    This looks like homework, so I’ll give you some hints to point you in the right direction, and you can fill-in the blanks. Try this for the first question:

    (define (loop lst acc)              ; receives a list and an accumulator
      (cond ((null? lst) <???>)         ; if the list is empty return the accumulator
            (else (loop <???> <???>)))) ; advance the recursion, +1 to accumulator
    
    (define (length1 lst)
      (loop <???> <???>))               ; how should we initialize the iteration?
    

    Try this for the second question:

    (define (length2 lst)
      (letrec ((loop <???>)) ; lambda with the same procedure as the previous `loop`
        (loop <???> <???>))) ; start the recursion exactly the same as in `length1`
    

    In any case, think for a moment: what’s the length of an empty (null) list? the answer will show you how to initialize the iteration. And for both solutions, we use an extra parameter called acc for keeping track of the answer so far, and we pass it along with the list to a looping tail-recursive procedure.

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

Sidebar

Related Questions

I'm trying to write two batch files that will allow me to switch the
I am trying to write a function that takes two containers of the same
I'm trying to write a piece of code that takes in a two digit
I am trying to write a bash script that looks at two files with
I'm trying to write an application (winforms) that can demonstrate how two oscillating colors
I'm trying to write an android application that has two main activities: login screen
Given a list of States, as in USA States, I am trying to write
I am trying to write my javascript functions in a separate file functions.js. Right
I'm trying to write two classes, Sale and Register, for my OO class. Here
I'm trying to write two applications (iphone and desktop) to achieve what's been described

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.