Exercise 12.3.1

2 views
Skip to first unread message

S Brown

unread,
Apr 24, 2009, 5:11:26 PM4/24/09
to Study-HTDP
I'm not sure how to do this exercise correctly.

My thoughts are as follows:

add-at-end needs to take a list, and return a new list, with the first
element of the list appended to the end of the new list. (IE - list
going in is 10 5 1, list coming out should be 10 5 1 10). My initial
solution was to require add-at-end to receive two arguments, an item
and a list, and have the specified item added to the end of the
specified list. So, in practice it ended up being used like this:

(add-at-end (first a-poly) a-poly)

This works, but I think I'm missing something here. I have tried going
through the design process, but I'm getting stuck because I haven't
been able to figure out another way of "retaining" that first value so
that it can be appended to the end. The only other solution I have
been able to come up with is something such as:

(define (add-at-end a-poly)
(add-at-end2 (first a-poly) a-poly))

where add-at-end2 is the same definition as the original add-at-end.


So, is there a better, or rather a more correct way of solving this
exercise, or is this solution acceptable?

Thanks.

Grant Rettke

unread,
Apr 25, 2009, 5:09:21 PM4/25/09
to Study-HTDP
Hi S Brown,

On Apr 24, 4:11 pm, S Brown <ontheh...@gmail.com> wrote:
> add-at-end needs to take a list, and return a new list, with the first
> element of the list appended to the end of the new list. (IE - list
> going in is 10 5 1, list coming out should be 10 5 1 10).

That is basically the same function description that I wrote for add-
at-end.

> My initial solution was to require add-at-end to receive two arguments, an item
> and a list, and have the specified item added to the end of the
> specified list. So, in practice it ended up being used like this:
>
> (add-at-end (first a-poly) a-poly)

This is a case where you might be writing a "wish list" as you go
along. You know add-at-end takes a single argument, a polygon, so
basically you might "wish" you had another function that took a
polygon, and something
you want to append to the end of it.

> This works, but I think I'm missing something here. I have tried going
> through the design process, but I'm getting stuck because I haven't
> been able to figure out another way of "retaining" that first value so
> that it can be appended to the end. The only other solution I have
> been able to come up with is something such as:
>
> (define (add-at-end a-poly)
>    (add-at-end2 (first a-poly) a-poly))
>
> where add-at-end2 is the same definition as the original add-at-end.

It is not exactly the same, because you have an additional argument:
the thing that you want to append to the end. This makes perfect
sense. I called the 2nd function "add-at-end-helper".

> So, is there a better, or rather a more correct way of solving this
> exercise, or is this solution acceptable?

It is acceptable.

S Brown

unread,
Apr 25, 2009, 7:48:29 PM4/25/09
to Study-HTDP
The "wish list" concept wasn't really clicking with me when I was
reading the chapter, but it makes total sense now. Thanks for the
feedback!
Reply all
Reply to author
Forward
0 new messages