Hi Todd & Marco -- <br><br>Thank you both for responding. <br><br>Perhaps my problem lies with my understanding of contracts? Here's what I have so far....<br>
<br>;contract for fold<br>;(X X > X) X (listof X) > X<br><br>;contract for abstract-from-sum and abstract-from-product<br>;(number number > number) number (listof numbers) > number<br><br>;contract for append<br>
;lst lst > lst<br><br>;contract for append-from-fold<br>;(lst lst > lst) lst lst > lst<br><br>;contract for map<br>;(X > Y) (listof X) > (listof Y)<br><br>I feel my understanding is running aground on two counts. First, it appears to me that my contract for append--<br>
<br>;contract for 'append<br>;(lst lst > lst) lst lst > lst<br>
<br>--does not "map" onto my contract for 'fold --<br><br>;contract for 'fold<br>;(X X > X) X (listof X) > X<br><br>After substituting 'append's 'lst parameter for all instances of X in 'fold, 'fold's third parameter becomes (listof lst). That can't be right? Is this where I'm floundering?<br>
<br>Second, I'm not sure how to resolve the discrepancy between the argument requirements for the function parameters in 'fold and 'map. 'Fold's function parameter requires two like arguments, and converts them into a like value (X X > X); 'map's function parameter requires only one argument, and coverts it into a different type of value (X > Y). So it would seem that the designer of map-from-fold is constrained by the structure of 'fold to make 'cons the argument for the function parameter, since it the only function that accepts two arguments and outputs a list. The final value of any list must be an empty list, so it seem that limits 'threshold's value to 'empty. The only parameter left is the list argument, and I suppose that's where the list from map goes. So the result is this....<br>
<br>(define(map f lox)<br> (fold cons empty lox))<br><br>... which simply outputs 'map's list parameter, returning me to square 1, and leaving 'f unused. <br><br>The only other thing I can think of is to take up Todd's suggestion, and start off 'map by applying 'f directly to (first lox) --<br>
<br>(define(map f lox)<br> (f((first lox).....<br><br>--'cons-ing that onto the front of a list <br><br>(define(map f lox)<br>
(cons(f((first lox)....<br><br>-- and then introducing 'fold in the manner above, with one change: making '(map f (rest lox)) the argument for 'fold's third parameter, since we know 'map, according to its' contract, is supposed to produce a list. <br>
<br>Hmm, that seems to work as long as the first conditional clause answers to 'empty:<br><br>(define(mapp2 f lox)<br> (cond<br> [(empty? lox)empty]<br> [else(cons(f(first lox))<br> (fold cons empty (mapp2 f (rest lox))))]))<br>
<br>(Sorry about the discursive prose. Writing this stuff out helps me work thru the problem-solving process....)<br><br>Could this be the solution sought after by HtDP? If so, it seems to me like a <i>much</i> more complicated version of the original 'map, or an instance of spending dollars to save pennies......<br>
<br>At any rate, your thoughts and suggestions are very much appreciated, especially since I'm trying to work thru HtDP on my own with assistance only from the members of plt-scheme.<br>
<br>Many thanks, <br>Dave Yrueta<br> <br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><div class="gmail_quote">On Mon, Jul 7, 2008 at 4:04 PM, Marco Morazan <<a href="mailto:morazanm@gmail.com" target="_blank">morazanm@gmail.com</a>> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div>On Mon, Jul 7, 2008 at 6:11 PM, dave yrueta <<a href="mailto:dyrueta@gmail.com" target="_blank">dyrueta@gmail.com</a>> wrote:<br>
><br>
</div><div>> (define(fold f th lox)<br>
> (cond<br>
> [(empty? lox)th]<br>
> [else<br>
> (f (first lox)<br>
> (fold f th (rest lox)))]))<br>
><br>
<br>
</div>What is the contract for fold?<br>
<div><br>
><br>
> (define(mapp2 f lox)<br>
> (fold (cons f) empty lox))<br>
><br>
> This won't work, because (cons f) is not a legitimate expression. I<br>
> started work on next problem in the meantime, and that seems even more<br>
> puzzling, so I feel like there is some basic concept about abstraction<br>
> that I'm not getting. Any suggestions?<br>
><br>
<br>
</div>Here's where a contract is going to be useful for you. What does fold<br>
expect as arguments? Certainly, (cons f) is not something fold is<br>
expecting as its first argument.<br>
<br>
Cheers,<br>
<font color="#888888"><br>
Marco<br>
</font></blockquote></div><br>