[racket] documentation example for slideshow/pict

From: Horace Dynamite (horace.dynamite at gmail.com)
Date: Sat Aug 7 16:58:51 EDT 2010

There is a mistake in your example, I've copied below a working one.
(renamed variable P to ph)

I think racket has excellent documentation. There seems to be a very
fine line between overly verbose documentation and concise
documentation.

Horace.

#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.
(define (print-sudoku S)
 (define (NL->pict nl)
   (if (list? nl)
       (table 3 (Build-list 9 (lambda (i)
                                (if (member i nl)
                                    (text (make-string 1 (digit->char i))
                                          "Courier" 16 0)
                                    (text " " "Courier" 16 0))))
              (cons cc-superimpose cc-superimpose)
              (cons cc-superimpose cc-superimpose)
              (cons 0 0) (cons 0 0))
       (inset (t (list->string (list  (digit->char nl)))) 20 0)))

 (let* ([unboxed-table (table 9 (map NL->pict (apply append S))
                              (cons cc-superimpose cc-superimpose)
                              (cons cc-superimpose cc-superimpose)
                              (cons 0 0) (cons 10 10))]
        [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)))))])
   (lt-superimpose
    unboxed-table thick-box-lines
    (rectangle (* pw (/ 1 9)) ph) (rectangle pw (* ph (/ 1 9)))
    (rectangle (* pw (/ 2 9)) ph) (rectangle pw (* ph (/ 2 9)))
    (rectangle (* pw (/ 3 9)) ph) (rectangle pw (* ph (/ 3 9)))
    (rectangle (* pw (/ 4 9)) ph) (rectangle pw (* ph (/ 4 9)))
    (rectangle (* pw (/ 5 9)) ph) (rectangle pw (* ph (/ 5 9)))
    (rectangle (* pw (/ 6 9)) ph) (rectangle pw (* ph (/ 6 9)))
    (rectangle (* pw (/ 7 9)) ph) (rectangle pw (* ph (/ 7 9)))
    (rectangle (* pw (/ 8 9)) ph) (rectangle pw (* ph (/ 8 9))))))

(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.