[plt-scheme] help writing a collector

From: Jordan Johnson (jmj at fellowhuman.com)
Date: Mon Apr 26 23:51:54 EDT 2010

Looking at the code, this looks like he might mean "collector" as in the "collect" functions towards the end of The Little Schemer, not a garbage collector...i.e., write a min&max function in CPS...

Joe, you said you want this:

>> (define collect-range
>>  (lambda (min max)
>>    (cons (min (cons max ' ())))))
>> (get-range ' (3 7 5 1 2 4) collect-range)

1) Have you written a get-min, for simpler contrast with get-max, yet?

2) So collect-range, your starting collector, takes two numbers.  That's fundamentally different from get-max's collector, because get-range is intended to return two answers.

3) Notice that when these functions recur, they create a new collector.  Where does that appear in the program, and what does (2) imply must happen at that point?

Good luck,

On Apr 26, 2010, at 1:48 PM, Ryan Culpepper wrote:

> What?
> It's hard to answer your question without knowing what approach you're trying to take. Your example, which would normally help, contains strange and undefined things like "get range".
> If you're following HTDP, then go back to the beginning and follow the design recipe. When you get stuck, the steps you've completed will help other people figure out what you're trying to do.
> (I suspect that you should read or reread at Part IV, Accumulating Knowledge. What information is "lost" when you recur; that is, what information about the elements you've already seen do you need to remember?)
> If you aren't following HTDP, you'll need to figure out some other way of talking about the design of your program. Good luck.
> Ryan
> Joe Burk wrote:
>> I know how to write a simple collector
>> (define collect-range
>>  (lambda (min max)
>>    (cons (min (cons max ' ())))))
>> But that will just return a list containing its two arguments.  I want my function to be able produce results such as these.
>> (get range ' (3 7 5 1 2 4) collect-range)  and it will return ' (1 7)
>> Ok so first off i have to define collect max. Which I do as follows
>> (define collect-max
>>   (lambda (max)
>>       max))
>> Next I have to define get max. Which is simply
>> (define get-max
>>  (lambda (lst collector)
>>    (cond ((null? lst) (collector 0))
>>             (else
>>              (get-max (cdr lst)
>>                (lambda (max)
>>                  (cond ((> (car lst) max)
>>                            (collector (car lst)))
>>                              (else
>>                               (collector max)))))))))
>> So the get max function will be able to keep track of the highest number in the list.
>> I need a function that can keep track of both the highest AND the number lowest in the list. If anyone could help me out I'd greatly appreciate it. Thanks!
>> ------------------------------------------------------------------------
>> _________________________________________________
>>  For list-related administrative tasks:
>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.