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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:14:23+00:00 2026-05-26T11:14:23+00:00

I’m having a hard time wrapping my head around recursion in PLT Scheme. For

  • 0

I’m having a hard time wrapping my head around recursion in PLT Scheme.

For example, if you have this code (unfinished), that outputs a number of “*”‘s and b number of “-“‘s:

(define (stars-and-stripes a b)
(local ((define repeat (lambda(w n) (cond [(= n 0) ""] [(> n 0) (string-append w (repeat w (− n 1)))]))))
(cond
[(= 0 a) (cons (repeat "-" b) empty)]
[(= 0 b) (cons (repeat "∗" a) empty)]
[ (and (> a 0) (> b 0)) ... (stars-and-stripes (− a 1) b) ... (stars-and-stripes a (− b 1))...])))

How can I trust that this part works correctly?

(stars-and-stripes (− a 1) b) ... (stars-and-stripes a (− b 1))

This is a conceptual concept I’ve had trouble grasping.

  • 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-26T11:14:24+00:00Added an answer on May 26, 2026 at 11:14 am

    Interesting question. The answer boils down to:

    You can trust it due to the principle of induction for natural numbers.
    

    That statement deserves an elaboration. Let us consider a simpler recursive function:

    (define (mult3 n)
      (cond
        [(= n 0) 0]
        [else    (+ 3 (mult3 (- n 1)))]))
    

    My claim is that (mult3 n) calculated n times 3 for all natural numbers n.
    How does one prove a statement ending in ” for all n” ?
    Well, one must use induction.

    The base case is n=0. What does (mult3 0) return? Since the n=0, the first cond-clause is used, and the result is 0. And since 3*0=0, we have that mult3 works in the base case.

    For the induction step one assumes that the property is true for all numbers less than n, and must prove that implies the property is true for n.
    In this case we must assume we can assume (mult3 n-1) returns 3*(n-1).

    With that assumption we must consider what (mult3 n) returns.
    Since m is not zero the second cond-clause is used. The value returned
    is (+ 3 (mult3 (- n 1))) and we have:

    (mult3 n)  = (+ 3 (mult3 (- n 1))) 
               = (+ 3 3*(n-1))         ; by induction hypothesis
               = 3*n
    

    For this example, your question is “How can I trust the expression (mult3 m-1)
    works correctly?”. The answer, is that you can trust it, due to the induction principle. In short: First check the base case(s), then check (mult3 n) under the assumption that (mult3 m) works for all m less than n.

    In order to use the induction principle, it is important that the argument to the recursive function besomes smaller. In the mult3 example to n becomes n-1.
    In the stars-and-stripe example either a or b becomes smaller in the recursive calls.

    Is this explanation making sense?

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

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into

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.