exer 12.3.1

1 view
Skip to first unread message

mike

unread,
Nov 19, 2008, 12:42:45 AM11/19/08
to Study-HTDP
To add the first element of a-poly structure to the end ; i used
(define (add-to-end a-poly)
(reverse (cons (first a-poly) (reverse a-poly))))

Ok it works but i used the function reverse as a guess; can someone
give me a hint on doing this without using (reverse list)?
thanks, mike

Geoffrey Lane

unread,
Nov 19, 2008, 9:52:55 PM11/19/08
to study...@googlegroups.com

Mike,
Think recursion.

What would you do if a-poly was an empty list?
Then what would you do if it's not empty?

Are those enough hints without giving it away? :)

--
Geoff Lane <geof...@gmail.com>

mike

unread,
Nov 19, 2008, 11:44:34 PM11/19/08
to Study-HTDP
What is getting me is that somehow i must extract (first a-poly) and
"put it somewhere"till i can add it to the end of the list. My prelim.
try was.
(define (add-to-end a-poly)
(cond
((empty? (rest a-poly)) (cons (first a-poly) (first a-poly))
(else (cons (first a-poly) (add-to-end a-poly)))))
so for the answer in the first cond line i have two (first a-poly)'s
; one for the original a-poly before recursion and the second one
represents (cons (make-posn a b) empty) at the end of the recursion.
So my problem is consing the original (first a-poly) to the end
of the a-poly structure.
thanks mike
ps i am a novice so i hope that i explained my thought process in
an understandable manner
> Geoff Lane <geoffl...@gmail.com>

mike

unread,
Nov 20, 2008, 12:13:38 AM11/20/08
to Study-HTDP
I guess that if i reply to myself that is a form of recursion.
Geoffrey here is the other way i wrote the function.

(define ele-1 (first poly-1))
(define (add-to-end-1 a-poly)
(cond
((empty? (rest a-poly)) (cons (first a-poly) (cons ele-1 empty)))
(else
(cons (first a-poly) (add-to-end-1 (rest a-poly))))))
If i define ele-1 outside the body of the function it works; but that
does
not seem elegant; so is there a way of defining ele-1 inside the body
of the
function? here is the implementation .


(add-to-end-1 poly-1)
(cons
(make-posn 10 10)
(cons (make-posn 60 60) (cons (make-posn 10 60) (cons (make-posn 10
10) empty))))
> poly-1
(cons (make-posn 10 10) (cons (make-posn 60 60) (cons (make-posn 10
60) empty)))
> So in the present form i must always redefine ele-1 outside the function body
ps i hope i am not overlooking something very obvious !!
thanks for our input
mike

Grant Rettke

unread,
Nov 20, 2008, 8:14:52 AM11/20/08
to Study-HTDP
On Nov 19, 8:52 pm, Geoffrey Lane <geoffl...@gmail.com> wrote:
> Think recursion.
>
> What would you do if a-poly was an empty list?
> Then what would you do if it's not empty?

Mike, show us your unit tests.
Reply all
Reply to author
Forward
0 new messages