[racket] documentation example for slideshow/pict

From: Bill Richter (richter at math.northwestern.edu)
Date: Wed Aug 11 23:50:33 EDT 2010

I solved my lined-box problem, and maybe `2.3 Pict Combiners' ought to
be explain it, as `Quick: An Introduction to Racket with Pictures'
uses slideshow.  New racket users will likely play with slideshow, and
e.g. try a table with horiz & vert lines separating the entries.

The documentation for table doesn't say what the col-aligns,
row-aligns, col-seps, and row-seps arguments actually do, but I think
a smart person who thought about it would realize that there's a grid
of horizontal & vertical line, and if e.g. each col-aligns &
row-aligns entry was cc-superimpose, then the centers of the entries
would all be on the grid intersection points.  And that if you
increase col-seps and row-seps, the entries will be pushed apart.  But
nothing in `2.3 Pict Combiners' indicates how you could make a lined
table.  It turns out to be easy:  superimpose each entry with a 
(rectangle max_w max_h)
where max_w & max_h are bigger than each width & height of all each
bounding box.   Then just make a table with the superimposed rectangle
entries, with each col-seps & row-seps entry 0.  Like this:


#lang slideshow

;; Digit = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
;; NL = number or list = Digit | (list of Digits)
;; Sudoku = (list of list of NL)

(define (digit->char i)
  (integer->char (+ 48 i)))

(define (Build-list n f) (build-list n (lambda (i) (f (add1 i)))))    

;; print-sudoku: Sudoku -> Pict
;; to build a picture of a sudoku as a 9x9 table, with all 9 boxes boxed, with a text picture for each big number, and a 3x3 table of text pictures for each empty cell list.  Each cell's entry (an NL) is centered in a square rectangle.  This causes the alignment and also produces the 9 row and 9 column lines.  
(define (print-sudoku S) 
  (define (NL->pict nl)
    (cc-superimpose 
     (rectangle 60 60)
     (if (list? nl)
         (table 3 (Build-list 
                   9 (lambda (i) 
                       (text (string-append " "
                                            (make-string 1 
                                                         (if (member i nl)
                                                             (digit->char i)
                                                             #\space)) 
                                            "  ") "Helvetica" 14 0)))
                (make-list 9 cc-superimpose) (make-list 9 cc-superimpose)
                (make-list 9 0) (make-list 9 0))
         (text (list->string (list  (digit->char nl))) "Helvetica" 32 0))))
  
  (let* ([unboxed-table 
          (table 9 (map NL->pict (apply append S))
                 (make-list 9 cc-superimpose) (make-list 9 cc-superimpose)
                 (make-list 9 0) (make-list 9 0))]
         [pw (pict-width unboxed-table)]
         [ph (pict-height unboxed-table)]
         [thick-box-lines 
          (linewidth 7 (lt-superimpose 
                        (rectangle pw ph)
                        (rectangle (/ pw 3) ph) (rectangle (* 2 (/ pw 3)) ph)
                        (rectangle pw (/ ph 3)) (rectangle pw (* 2 (/ ph 3)))))])
    (cc-superimpose unboxed-table thick-box-lines)))

(define Unsolvable#7
  '((8 (3 7) (3 5 7) 2 (3 5) 1 9 4 6)
    (2 4 6 (7 8) 9 (5 7 8) (5 7) 1 3)
    ((3 5) 1 9 (3 7) 4 6 (5 7) 8 2)
    (1 2 4 (3 6 8) (3 8) (5 7) (3 6) (5 7) 9)
    ((3 5 7) (3 6 7) 8 9 2 4 (3 6) (5 7) 1)
    (9 (3 6 7) (5 7) (1 6 7) (1 3 5 6) (3 7) 8 2 4)
    ((3 6 7) 5 (2 3 7) 4 (3 6) (2 3) 1 9 8)
    (4 8 (1 3) (1 3 6) 7 9 2 (3 6) 5)
    ((3 6) 9 (1 2) 5 (1 8) (2 8) 4 (3 6) 7)))

(print-sudoku Unsolvable#7)


Posted on the users mailing list.