[plt-scheme] HTDP Exercise 9.5.7

From: Marco Morazan (morazanm at gmail.com)
Date: Mon Apr 13 16:32:11 EDT 2009

On Mon, Apr 13, 2009 at 12:12 PM, S Brown <ontheheap at gmail.com> wrote:
> I'm working my way through HTDP and I have a question regarding
> Exercise 9.5.7. The exercise asks you to define the function average-
> price, which takes as it's input a list of prices, and gives as output
> the average of the prices in the list. I came up with  the following
> solution:
> I came to this solution after not being able to figure out how to keep
> track of the sum of the items and also the number of items at the same
> time. So my concern/question is, is my solution acceptable, or should
> I keep trying to figure out how to keep track of both values (sum and
> number of items) without resulting to helper-functions such as sum-
> list and num-items? Thanks.

I am going to give you different feedback.

Where does the idea of keeping track of both values come from? At this
point in HtDP, there is nothing in the DR to suggest that values
should be or can be "kept track of." You write programs based on the
shape of your input as done in your solution. Nothing else is
required. This may seem to suggest that one ought to have "tunnel
vision" when following the steps of the DR and at this point yes you
should. :-) Revisit this problem when you cover accumulator-style
recursion. It is at that point that the idea of keeping track of
values is best introduced. I speculate that your reasoning steps are
screaming to keep track of values, because they are "contaminated" by
an imperative bias (that hunger is properly satisfied in sections VII
and VIII and IMHO they are not fun as judged by the reaction of my

A rule of thumb that is useful is to remember that if you need to
examine data of arbitrary size to produce an intermediate result then
that work should be done by an auxilary function (as in the solution
you presented).




Posted on the users mailing list.