[plt-scheme] help on HTDP ex-9.5.8

From: arnuld (arnuld3 at yahoo.co.in)
Date: Mon Sep 26 06:47:35 EDT 2005

Hi there,

          i wrote a solution to the ex 9.5.8 of HTDP.
I was not able to develop the FULL solution. So i have
PARTIAL SOLUTION only. can you help me on fixing this
problem?

I willbe really thankfull to you.

here i am providing "PROBLEM STATEMENT" & "my PARTIAL
solution" too. Please have a look.

***************************************************************************
Exercise 9.5.8.   Develop the function draw-circles,
which consumes a posn p and a list of numbers. Each
number of the list represents the 
radius of some circle. The function draws concentric
red circles around p on a canvas, using the operation
draw-circle. Its result is true, if it can draw all of
them; otherwise an error has occurred and the function
does not need to produce a value.

Use the teachpack draw.ss; create the canvas with
(start 300 300). 

***************************************************************************

;; DATA-DEFINITION:
;; a list-of-numbers is either:
;;      1. an empty list, <empty>, or
;;      2. <(cons n lon)>, where n= number, lon=
list-of-numbers

;; draw-circles : posn, list-of-numbers -> true
;; to draw concentric-red-circles around "posn" with
radii "list-of-numbers"
;; (define (draw-circles p lon)....)

;; EXAMPLES:
;; (draw-circles (make-posn 100 10) (cons 10 (cons 8
empty))) = true, true
;; (draw-circle (make-posn 50 50) (cons -19 (cons 10
empty))) = true

;; TEMPLATE: f-template : posn, lon -> ???
;;(define (f-template posn, lon)
  ;; (cond
   ;; [(empty? lon)..]
   ;; [else...
   ;;  (cond
   ;;    [(<= (first lon) 0)...]
   ;;    [else....(first lon)... (f-template posn
(rest lon))...])]))

;; BODY
(start 300 300)
(define (draw-circles posn lon)
  (cond
    [(boolean=? (check-integer lon) false) false] 
    [else (and (draw-circle (make-posn (posn-x posn)
(posn-y posn)) (first lon) 'red)
               (sleep-for-a-while 2)
               (draw-circles posn (rest lon)))]))

(define (check-integer lon)
  (cond
    [(<= (first lon) 0) false]
    [(empty? (rest lon)) true]
    [else (check-integer (rest lon))]))


;; test
"true" ;; this is for (start 300 300)
;;test-1
(draw-circles (make-posn 100 100) (cons 20 (cons 30
(cons 10 empty)))) "= true"
;; test-2
(draw-circles (make-posn 50 50) (cons 10 (cons -20
empty))) 

;; test-2 is done with "as it has -ve radius" and it
DOES NOT produce any value "as required by the
programme". 
;; the only problem left is of "LAST <empty>" which
RAISES EROOR after drawing all the circles....i.e.
;; (draw-circles (make-posn 100 100) (cons 20 (cons 30
(cons 10 empty)))), PROCEEDS AS:....
;; FIRST CIRCLE       is drawn with RADIUS = 20....OK
;; SECOND CIRCLE     IS DRAWN WITH radius = 30....OK
;; PROGRAMME also PICKS-UP <empty> as THIRD CIRCLE and
then RAISES error as shown in the "2ND-LAST-LINE" of
OUTPUT
;; I am not able to solve this Problem.

;;############### OUTPUT ##################
Welcome to DrScheme, version 209.
Language: Beginning Student.
Teachpack: /usr/local/plt/teachpack/htdp/draw.ss.
true
"true"
first: expects argument of type <non-empty list>;
given empty
> 
;;############## E.O.PROGRAMME ###################

THANKS for spending time on READING into my problem.

ARNULD


"the Great Intellectuals"


		
__________________________________________________________ 
Yahoo! India Matrimony: Find your partner now. Go to http://yahoo.shaadi.com


Posted on the users mailing list.