[plt-scheme] HTDP 21.1.2

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Sun Jul 16 02:26:12 EDT 2006

> mysteryFunction corresponds corresponds to the (X Y -> Y) so unifying 
> the contract with the known value of Y
> (X [listof symbol] -> [listof symbol])

Hi Wooks,

Yup, that's exactly how we know mystery-function has to return a list of 

> Now it looks like setting X to symbol results from an educated guess 
> because I don't see anything that strictly binds X to symbol (X could be 
> a number and mystery function might be a function that returns a list of 
> X randomly selected symbols (from the input Y).

Ok, fair enough.  Let's look at our definitions and see if we can figure 
it out.  Let's scratch out the part of the contract that we're not quite 
sure of yet, and to make things easier to see, let's summarize what we 
know so far:

;; fold: (X Y -> Y) Y (listof X) -> Y
(define (fold f acc a-lst)

;; double-up: (listof symbol) -> (listof symbol)
(define (double-up lst)
   (fold mystery-function empty lst))

;; mystery-function: XXX (listof symbol) -> (listof symbol)
(define (mystery-function x acc)

I've put XXX on the mystery function's contract on its first input, since 
we don't know yet what XXX is.  Do you agree with the scenario above, with 
the contracts as they stand?

If so, would you be able to fill in what XXX is now at this point?  If so, 
how?  And if not, we'll go deeper.  *grin*

>> So the mystery-function here is going to have a very simple definition. 
>> Would you be able to fill that definition in?
> (define (mysteryfunction sym alos)
> (cons sym (cons sym alos)))

A few more questions:

Does it work?

If so, does this surprise you?  Remember, you initially thought that fold 
does only summarization, the kind that takes a list of things and produces 
a singular, non-listy value.  But this is an example of a fold that eats a 
list of symbols and also produces a list of symbols.  Granted, the output 
list ends up being larger than the input list, but still, I hope this 
helps clear that misconception up.

Finally... well, is this helping?  *grin*

Seriously, I just want to make sure that this detour makes sense to you. 
The point of the exercise here is to give you some practice using fold on 
a slightly different-but-similar example, to help build you up so you can 
do 21.1.2.

Best of wishes!

Posted on the users mailing list.