[racket] HtDP, andmap & ormap

From: Bill Richter (richter at math.northwestern.edu)
Date: Mon Oct 4 15:54:10 EDT 2010

My son's Racket Sudoku Solver extensively uses ormap to search a list,
and if it finds a match, return something good, else return false
http://schemecookbook.org/Cookbook/SudokuSolver.  I'm interested in
know how HtDP recommends designing such programs.

There are no uses of ormap in HtDP, other than the Figure 57
definition, but andmap is claimed to be useful in
http://www.htdp.org/2003-09-26/Book/curriculum-Z-H-32.html#node_idx_1676

   Exercise 25.1.2.  Develop move-balls. The function consumes a list of
   balls and moves each one until all of them have moved out of bounds.
   Hint: It is best to write this function using filter, andmap, and
   similar abstract functions from part IV.

I think that andmap is not needed, and we only need filter & map:

(define WIDTH 500)
(define HEIGHT 500)
(define DELAY .1)

;; move-balls: (list Ball) -> true
;; to consumes a list of balls and moves each one until all of them have moved 
;; out of bounds
(define (move-balls loBalls)
  (let ([active-balls 
         (filter (lambda (a-ball)
                   (not (out-of-bounds? a-ball)))
                 loBalls)])
    (if (empty? active-balls)
      true      
      (begin (map draw-and-clear active-balls)
             (move-balls (map move-ball active-balls))))))

(start WIDTH HEIGHT)
(move-balls (list (make-ball 100 200 -5 +17)
                  (make-ball 300 100 10 0)
                  (make-ball 200 200  5 +17)
                  (make-ball 400 300 -5 -17)
                  (make-ball 400 100  5 -17)
                  (make-ball 240 150  10 10)))
(stop)

It's fun to rewrite this exercise so each ball has a different color.  


Posted on the users mailing list.