[racket] documentation example for slideshow/pict

From: Bill Richter (richter at math.northwestern.edu)
Date: Thu Aug 5 17:16:35 EDT 2010

Dr Racket is proving to be a very good way for my 11-yr old son Ben to
learn to program, and he wrote a simple Sudoku solver using graphics
from slideshow/pict, whose documentation is cryptic as it contains no
examples.  Here's an example you might consider including: Ben's
`print-sudoku' function.  I might add that the documentation in 2.3
Pict Combiners doesn't actually say what col-aligns & row-aligns do.
I couldn't find where it said that picts can be printed with `print',
but not `display' or `pretty-print'.

#lang slideshow

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

;; digit->char: digit -> character
;; to turn a digit i into #\i 
(define (digit->char i)
  (integer->char (+ 48 i)))

(define (print-sudoku S)  
  (define (NL->PictText nl)
    (if (list? nl)
        (text (string-append "  " (list->string (map digit->char nl))
                             "  ") "Helvetica" 16 0)
        (text (string-append " " (make-string 1 (digit->char nl)) " ")
              "Helvetica" 32 0)))  
  (let* ([table-without-boxes
          (table 9 (map NL->PictText (apply append S))
                 (cons ct-superimpose ct-superimpose) 
                 (cons cc-superimpose cc-superimpose)
                 (cons 40 40) (cons 0 0))]
         [pw (pict-width table-without-boxes)]
         [ph (pict-height table-without-boxes)])
    (lt-superimpose 
     table-without-boxes (rectangle pw ph) 
     (rectangle (/ pw 3) ph) (rectangle (* 2 (/ pw 3)) ph)
     (rectangle pw (/ ph 3)) (rectangle pw (* 2 (/ ph 3))))))

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

(print-sudoku S6)


Posted on the users mailing list.