[racket] HtDP, andmap & ormap

From: David Van Horn (dvanhorn at ccs.neu.edu)
Date: Mon Oct 4 17:09:47 EDT 2010

On 10/4/10 3:54 PM, Bill Richter wrote:
> 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.

I would first write a program like this:

;; [Maybe X] = X or false

;; [Listof String] -> [Maybe String]
(define (first-long-line los)
   (cond [(empty? los) false]
         [(> (string-length (first los)) 80)
          (first los)]
         [else
          (first-long-line (rest los))]))

Later, I might write a program like:

;; [Listof Number] -> [Maybe Number]
(define (first-even los)
   (cond [(empty? los) false]
         [(even? (first los))
          (first los)]
         [else
          (first-even (rest los))]))

Then I would observe the pattern and abstract following the recipe in 
Section 21.  The pattern captured in this abstraction is *not* what's 
captured in HtDP's ormap, but it is what's captured in Racket's ormap.

> 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 would avoid draw.ss and use universe instead (but ormap and the 
abstraction of the above functions are both useful regardless).

David


Posted on the users mailing list.