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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:52:57+00:00 2026-05-13T09:52:57+00:00

I have been working alongside The Little Schemer to learn Scheme and using PLT-Scheme

  • 0

I have been working alongside The Little Schemer to learn Scheme and using PLT-Scheme for my environment.

The Little Schemer has helped me tremendously with recursion (it is straightforward for me now) but I’m stuck on a portion of the book that introduces “collectors” and calls the function as a whole a continuation.

Here is the example code they have used. I understand the recursive elements but I am stuck, in particular on the lambda functions – my mind can’t follow the path and how the arguments for that lambda function are set (since their only call is to call them again in recursion, there is no concrete use within the function body).

If someone could more-or-less give me a break down of the path of computation through the recursion of the function into the lambda collectors, that may help me.

;; Build a nested list of even numbers by removing the odd ones from its
;; argument and simultaneously multiply the even numbers and sum the odd
;; numbers that occur in its argument.
(define (even-only-collector l col)
  (cond
    ((null? l)
      (col (quote ()) 1 0))
    ((atom? (car l))
      (cond
        ((even? (car l))
          (even-only-collector (cdr l)
            (lambda (newl p s)
              (col (cons (car l) newl)
                (* (car l) p) s))))
         (else
           (even-only-collector (cdr l)
             (lambda (newl p s)
               (col newl
                 p (+ (car l) s)))))))
    (else
      (even-only-collector (car l)
        (lambda (al ap as)
          (even-only-collector (cdr l)
            (lambda (dl dp ds)
              (col (cons al dl)
                (* ap dp)
                (+ as ds)))))))))

;; The collector function
(define (collector newl product sum)
  (cons sum
    (cons product newl)))

Thank you in advance!!

  • 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-13T09:52:58+00:00Added an answer on May 13, 2026 at 9:52 am

    Try something simpler to see how this works. For example, here’s a version of a list-sum function that receives a continuation argument (which is often called k):

    (define (list-sum l k)
      (if (null? l)
        ???
        (list-sum (cdr l) ???)))
    

    The basic pattern is there, and the missing parts are where the interesting things happen. The continuation argument is a function that expects to receive the result — so if the list is null, it’s clear that we should send it 0, since that is the sum:

    (define (list-sum l k)
      (if (null? l)
        (k 0)
        (list-sum (cdr l) ???)))
    

    Now, when the list is not null, we call the function recursively with the list’s tail (in other words, this is an iteration), but the question is what should the continuation be. Doing this:

    (define (list-sum l k)
      (if (null? l)
        (k 0)
        (list-sum (cdr l) k)))
    

    is clearly wrong — it means that k will eventually receive the the sum of (cdr l) instead of all of l. Instead, use a new function there, which will sum up the first element of l too along with the value that it receives:

    (define (list-sum l k)
      (if (null? l)
        (k 0)
        (list-sum (cdr l) (lambda (sum) (+ (car l) sum)))))
    

    This is getting closer, but still wrong. But it’s a good point to think about how things are working — we’re calling list-sum with a continuation that will itself receive the overall sum, and add the first item we see now to it. The missing part is evident in the fact that we’re ignoring k. What we need is to compose k with this function — so we do the same sum operation, then send the result to k:

    (define (list-sum l k)
      (if (null? l)
        (k 0)
        (list-sum (cdr l) (compose k (lambda (s) (+ s (car l)))))))
    

    which is finally working. (BTW, remember that each of these lambda functions has its own “copy” of l.) You can try this with:

    (list-sum '(1 2 3 4) (lambda (x) x))
    

    And finally note that this is the same as:

    (define (list-sum l k)
      (if (null? l)
        (k 0)
        (list-sum (cdr l) (lambda (s) (k (+ s (car l)))))))
    

    if you make the composition explicit.

    (You can also use this code in the intermediate+lambda student language, and click the stepper button to see how the evaluation proceeds — this will take a while to go over, but you’ll see how the continuation functions get nested, each with it’s own view of the list.)

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

Sidebar

Related Questions

I've recently been working on a project using WPF to produce a diagram. In
I'm currently working with PHPUnit to try and develop tests alongside what I'm writing,
I have an asp.net MVC app that i am working on and I wrote
I am working on an application which accepts any uploaded CSV data, stores it
I've just started working for a new firm as an in-house web developer. Up
I want to use the FTP task in ant, and I have found the
I've set up Subversion using Beanstalk + Coda for my web app, which up
What is the best practice of introducing custom (typically volatile) data into entity model
I'm developing on Windows, and I've searched everywhere without finding anyone talking about this

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.