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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:37:30+00:00 2026-06-12T13:37:30+00:00

I have to define a variadic function in Scheme that takes the following form:

  • 0

I have to define a variadic function in Scheme that takes the following form:
(define (n-loop procedure [a list of pairs (x,y)]) where the list of pairs can be any length.

Each pair specifies a lower and upper bound. That is, the following function call: (n-loop (lambda (x y) (inspect (list x y))) (0 2) (0 3)) produces:

(list x y) is (0 0)
(list x y) is (0 1)
(list x y) is (0 2)
(list x y) is (1 0)
(list x y) is (1 1)
(list x y) is (1 2)

Obviously, car and cdr are going to have to be involved in my solution. But the stipulation that makes this difficult is the following. There are to be no assignment statements or iterative loops (while and for) used at all.

I could handle it using while and for to index through the list of pairs, but it appears I have to use recursion. I don’t want any code solutions, unless you feel it is necessary for explanation, but does anyone have a suggestion as to how this might be attacked?

  • 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-12T13:37:32+00:00Added an answer on June 12, 2026 at 1:37 pm

    The standard way to do looping in Scheme is to use tail recursion. In fact, let’s say you have this loop:

    (do ((a 0 b)
         (b 1 (+ a b))
         (i 0 (+ i 1)))
        ((>= i 10) a)
      (eprintf "(fib ~a) = ~a~%" i a))
    

    This actually get macro-expanded into something like the following:

    (let loop ((a 0)
               (b 1)
               (i 0))
      (cond ((>= i 10) a)
            (else (eprintf "(fib ~a) = ~a~%" i a)
                  (loop b (+ a b) (+ i 1)))))
    

    Which, further, gets macro-expanded into this (I won’t macro-expand the cond, since that’s irrelevant to my point):

    (letrec ((loop (lambda (a b i)
                     (cond ((>= i 10) a)
                           (else (eprintf "(fib ~a) = ~a~%" i a)
                                 (loop b (+ a b) (+ i 1)))))))
      (loop 0 1 0))
    

    You should be seeing the letrec here and thinking, “aha! I see recursion!”. Indeed you do (specifically in this case, tail recursion, though letrec can be used for non-tail recursions too).

    Any iterative loop in Scheme can be rewritten as that (the named let version is how loops are idiomatically written in Scheme, but if your assignment won’t let you use named let, expand one step further and use the letrec). The macro-expansions I’ve described above are straightforward and mechanical, and you should be able to see how one gets translated to the other.


    Since your question asked how about variadic functions, you can write a variadic function this way:

    (define (sum x . xs)
      (if (null? xs) x
          (apply sum (+ x (car xs)) (cdr xs))))
    

    (This is, BTW, a horribly inefficient way to write a sum function; I am just using it to demonstrate how you would send (using apply) and receive (using an improper lambda list) arbitrary numbers of arguments.)


    Update

    Okay, so here is some general advice: you will need two loops:

    1. an outer loop, that goes through the range levels (that’s your variadic stuff)
    2. an inner loop, that loops through the numbers in each range level

    In each of these loops, think carefully about:

    1. what the starting condition is
    2. what the ending condition is
    3. what you want to do at each iteration
    4. whether there is any state you need to keep between iterations

    In particular, think carefully about the last point, as that is how you will nest your loops, given an arbitrary number of nesting levels. (In my sample solution below, that’s what the cur variable is.)

    After you have decided on all these things, you can then frame the general structure of your solution. I will post the basic structure of my solution below, but you should have a good think about how you want to go about solving the problem, before you look at my code, because it will give you a good grasp of what differences there are between your solution approach and mine, and it will help you understand my code better.

    Also, don’t be afraid to write it using an imperative-style loop first (like do), then transforming it to the equivalent named let when it’s all working. Just reread the first section to see how to do that transformation.

    All that said, here is my solution (with the specifics stripped out):

    (define (n-loop proc . ranges)
      (let outer ((cur ???)
                  (ranges ranges))
        (cond ((null? ranges) ???)
              (else (do ((i (caar ranges) (+ i 1)))
                        ((>= i (cadar ranges)))
                      (outer ??? ???))))))
    

    Remember, once you get this working, you will still need to transform the do loop into one based on named let. (Or, you may have to go even further and transform both the outer and inner loops into their letrec forms.)

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

Sidebar

Related Questions

C++0x thread library or Boost.thread define a non-member variadic template function that locks all
I have come across the need to reorder a variadic list of parameters that
I have define the following SCPD document for my UPnP device: <?xml version=1.0?> <scpd
I have to define a list in which: 1 is a member if n
I can't link properly to glew. I have done: #define GLEW_STATIC #include glew/glew.h #pragma
So I have this function: (define (try try-block catch-block finally-block) ; Implements try/catch/finally like
I have a logging function which accepts variadic parameters. This works fine for say
I have #define values in headers that I certainly want Doxygen to document but
I have a variadic function: LogWrite(FILE * fp, int level, const char * filename,
I have a variadic template member function defined as: template<typename ... Params> VAlgorithm* CreateAlgorithm(const

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.