fork download
  1. ; latin squares
  2.  
  3. (define (take n xs)
  4. (let loop ((n n) (xs xs) (ys '()))
  5. (if (or (zero? n) (null? xs))
  6. (reverse ys)
  7. (loop (- n 1) (cdr xs)
  8. (cons (car xs) ys)))))
  9.  
  10. (define (drop n xs)
  11. (let loop ((n n) (xs xs))
  12. (if (or (zero? n) (null? xs)) xs
  13. (loop (- n 1) (cdr xs)))))
  14.  
  15. (define (range . args)
  16. (case (length args)
  17. ((1) (range 0 (car args) (if (negative? (car args)) -1 1)))
  18. ((2) (range (car args) (cadr args) (if (< (car args) (cadr args)) 1 -1)))
  19. ((3) (let ((le? (if (negative? (caddr args)) >= <=)))
  20. (let loop ((x(car args)) (xs '()))
  21. (if (le? (cadr args) x)
  22. (reverse xs)
  23. (loop (+ x (caddr args)) (cons x xs))))))
  24. (else (error 'range "unrecognized arguments"))))
  25.  
  26. (define (shuffle x)
  27. (do ((v (list->vector x)) (n (length x) (- n 1)))
  28. ((zero? n) (vector->list v))
  29. (let* ((r (random n)) (t (vector-ref v r)))
  30. (vector-set! v r (vector-ref v (- n 1)))
  31. (vector-set! v (- n 1) t))))
  32.  
  33. (define (rot n xs)
  34. (if (negative? n)
  35. (set! n (- (length xs) (- n))))
  36. (append (drop n xs) (take n xs)))
  37.  
  38. (define (latin n)
  39. (do ((n n (- n 1))
  40. (m (list (shuffle (range n))) (cons (rot 1 (car m)) m)))
  41. ((= n 1) (shuffle (apply map list (shuffle m))))))
  42.  
  43. (define (f2 n)
  44. (let ((s (number->string n)))
  45. (if (< n 10) (string-append " " s) s)))
  46.  
  47. (for-each (lambda (row) (display (map f2 row)) (newline)) (latin 25))
Success #stdin #stdout 0.02s 50632KB
stdin
Standard input is empty
stdout
(10  6  0 17 21 13 14  1  5  9  4 22 19 12 18 20  2 15 24 16  7 11  8 23  3)
( 4 19 12 14  9 15 22  3 11  5  1  7 18  6  0 16 17 20 10 23  2  8 21 13 24)
(22 20 13 21 19 24  9  2  0 18  7  5 16 15 23  4  8 10 14  1 11 12  6  3 17)
(15  8  5  3 17  0 24 16 22 14 20 10 21 11  9  6  1 12 13 19  4  7  2 18 23)
( 0  7 14 16  1  9 23  6 24  3 12 13  2 22 17 11 20  5 18  8 15 10  4 21 19)
(24 12 18  2  8 23 17  4  9 21 10 14  6  0 19 15  7 13  3 20 22  5 11 16  1)
( 5  4 24 19 16 14 18  8 13 23 11  0  1 10  3  7  6 22  9  2 12 15 20 17 21)
(19 14  2 15 10  8 20  0  1  4 18 16 22 17  7  9 13 21  6  5 23  3 24 11 12)
(16  9  8 10 22  6  4 13  2  7 23  1  5 21 11 18 24 19 20  0  3 17 14 12 15)
(18 22 17 20  4 21 16 12  3  1  0 23  7 14  2  5 15  9 19 11 13 24 10  8  6)
( 1 18  6 22  5 20  7 24  8 11  3  2  0 19 12 23 14 16  4 13 17 21  9 15 10)
(14 15 23  8  6  3 21  7 18 19 22  9 20 13 16 10 11 24 17  4  5  0 12  1  2)
( 9 10  3  6 20 17 19 11 23 16  5 18  4 24  1 22 12 14 21  7  0 13 15  2  8)
(17 13 16 11 12  1  8 22 19  6 14 21 15 23 20 24  5  3  2 10  9 18  0  4  7)
(23  5 21  4  7 19  1 15 17  2 13  3 11  9  8  0 10 18 16 12 24 14 22  6 20)
(12  2 22 23  3  5 13 19 10 24  6 15 17  7 14  8 16 11  0 21 20  4  1  9 18)
( 7 16 15  9 18 10  5 17 12  0  2 11 23 20 13  1 21  4 22  3  8  6 19 24 14)
(20 21 11 24 14 12 10 23  7 22 16  4  9  8  5 19  3  6 15 18  1  2 17  0 13)
(13 11  9  1  2 18  3 20 14 17 15 24  8  5 21 12  4  0 23  6 10 22  7 19 16)
(21 24  1 12 15  2  6  5 16 20  9 19 10  3  4 14  0 17  8 22 18 23 13  7 11)
(11  1 10 18 23 22  0 21 15 13  8 12  3  4 24  2 19  7  5 17  6 20 16 14  9)
( 3  0 19  7 11 16  2 10 21  8 24 17 12 18  6 13 22 23  1 15 14  9  5 20  4)
( 8  3  4  0 13  7 12  9 20 15 21  6 24  1 10 17 18  2 11 14 19 16 23 22  5)
( 6 17  7 13 24 11 15 18  4 10 19 20 14  2 22 21 23  8 12  9 16  1  3  5  0)
( 2 23 20  5  0  4 11 14  6 12 17  8 13 16 15  3  9  1  7 24 21 19 18 10 22)