[plt-scheme] HTDP 21.1.2

From: wooks . (wookiz at hotmail.com)
Date: Sun Jul 16 08:45:54 EDT 2006

>
>>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?
>

Yup.

>If so, would you be able to fill in what XXX is now at this point?

Yup.

>If so, how?  And if not, we'll go deeper.  *grin*

the 3rd parameter of fold is [listof X] which was instantiated to [listof 
symbols] hence X is bound to be a symbol...... How did folds 3rd parameter 
come to be instantiated to [listof symbols] - we are using fold to define 
double-up and double-up's contract stipulates [listof symbols] as input - 
hence thats what the 3rd parameter of fold has to be.
>
>
>
>>>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?
>

yes.

>If so, does this surprise you?

no

>Remember, you initially thought that fold does only summarization, the kind 
>that takes a list of things and produces a singular, non-listy value.

The possible solution space has been narrow by a deductive derivation from 
the way the contracts have to be instantiated.  If Sherlock Holmes were a 
programmer it would be how he would proceed.


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

Sure it's a great heuristic

>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.
>

I did my best ... but I guess my best wasn't good enough - (George Benson - 
Just Once).




Posted on the users mailing list.