[plt-scheme] newbie: student-riot exercise
The program works as I imagined. But I have three objections:
1. You clearly don't use the design recipe.
2. Eh, you clearly don't use the design recipe.
3. Did I mention that you clearly don't use the design recipe?
Here is an example of where you go wrong badly:
(define (riot n)
(cond
[(zero? n) empty]
[else (cons radius (riot (sub1 n)))]))
(define (draw-balloons a-lon)
(cond
[(empty? a-lon) true]
[else (draw-all a-lon)]))
(define (draw-all a-lon)
(and (draw-solid-disk (make-posn (random canvas-x)
(random canvas-y))
(first a-lon)
'red)
(sleep-for-a-while 1)
(draw-balloons (rest a-lon))))
What you really want is this:
;; riot : NaturalNumber -> true
;; draw n balloons into the grid at random locations
(define (riot n) true)
The function definition is wrong. Now improve it. Make up an example
input. Imagine what it should do. Do the template. Fill in the gaps.
Watch it work and have a third of the program text. That is easier to
comprehend, to understand why it works, and to modify in case the
students start throwing other stuff too or you want additional things
in the classroom.
-- Matthias
On Jan 26, 2006, at 4:57 AM, arnuld wrote:
> hello everyone,
> i have created a programme which works
> fine and not in the way intended by problem statement. here is the
> problem statement copied as it is from HtDP.
>
> -- Exercise 11.3.5. Develop a program that visualizes a student
> riot. In preparation of a student riot, a small group of students
> meets to make paint-filled balloons. The typical riot uses 'red only.
> Then, on the evening of the riot, the students enter a university's
> progressive theater with the balloons and throw them all over the
> seats.
>
> The program's only input should be a natural number, which represents
> the number of balloons thrown. The visualization should use a canvas
> that contains a black grid and the positions of the balls.Assume a
> random distribution of the balls over the theater's seats. Each box in
> the grid represents a seat. Configure the program so the change of one
> variable definition changes the number of columns in the grid and a
> change to another changes the number of rows.
>
> Hint: Develop auxiliary functions that draw some given number of lines
> in the vertical and the horizontal direction. ----
> my solution works not with function call : (riot n)
> but
> with
> (draw-baloons (riot n))
>
> also < riot > consumes only one agument but i had to use radius of
> disk (red baloons) as an extra in its body as shown down here.
>
> may you help me on modifying <riot > according to the problem
> statement?
>
> "arnuld"
>
> ---------------------- start of programme
> -------------------------------------------
> (define ROWS 5)
> (define COLS 5)
> ;; i created variable definitions of COLS & ROWS because problem wants
> so
> ;; hence you can change the values of COLS & ROWS here in variable
> definitions and effect will be
> ;; transfered into the variables & functions which use these 2
> variables
> ;; same is for canvas
>
> (define canvas-x 300)
> (define canvas-y 300)
> (define dist-ROWS (quotient canvas-y ROWS))
> (define dist-COLS (quotient canvas-y COLS))
> (define radius 5)
>
> (define (riot n)
> (cond
> [(zero? n) empty]
> [else (cons radius (riot (sub1 n)))]))
>
> (define (draw-balloons a-lon)
> (cond
> [(empty? a-lon) true]
> [else (draw-all a-lon)]))
>
> (define (draw-all a-lon)
> (and (draw-solid-disk (make-posn (random canvas-x)
> (random canvas-y))
> (first a-lon)
> 'red)
> (sleep-for-a-while 1)
> (draw-balloons (rest a-lon))))
>
> ;; ------------ draw columns and rows here
> -------------------------------
>
> (define (draw-COLS COLS)
> (cond
> [(zero? COLS) true]
> [else (and (draw-solid-line (make-posn (* COLS dist-COLS) 0)
> (make-posn (* COLS dist-COLS)
> canvas-y) 'black)
> (draw-COLS (sub1 COLS)))]))
>
> ;; change the values of COLS & ROWS in variable definitions and effect
> will be here into these
> ;; functions
>
> (define (draw-ROWS ROWS)
> (cond
> [(zero? ROWS) true]
> [else (and (draw-solid-line (make-posn 0 (* ROWS dist-ROWS))
> (make-posn canvas-x (* ROWS
> dist-ROWS)) 'black)
> (draw-ROWS (sub1 ROWS)))]))
>
> ;; tests
> (start canvas-x canvas-y)
> (draw-COLS COLS)
> (draw-ROWS ROWS)
>
> (draw-balloons (riot 20))
>
> (sleep-for-a-while 1)
> (stop)
>
> ------------------------ end of programme
> ------------------------------------------
>
> --
> "the great
> intellectuals"_________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme