59 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			59 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
| (define nums '(1 2 3 4 5 6 7 8 9))
 | |
| 
 | |
| (defun amount (x y)
 | |
|   (foldr (lambda (z q) (if (eq x z) (+ q 1) q)) 0 y))
 | |
| 
 | |
| (defun valid (x)
 | |
|     (if (= 9 (length x))
 | |
|         (foldr
 | |
|             (lambda (y z) (if (<= y 1) z nil))
 | |
|             t
 | |
|             (map (lambda (y) (amount y x)) nums))
 | |
|         nil))
 | |
| 
 | |
| (defun getRow (grid x) (ix grid x))
 | |
| 
 | |
| (defun transpose (grid)
 | |
|   (match grid
 | |
|     (nil nil)
 | |
|     (((? x))
 | |
|         (map (lambda (y) (list y)) x))
 | |
|     (((? x) . (? xs))
 | |
|      (if (list? x)
 | |
|         (zipWith
 | |
|             cons
 | |
|             x
 | |
|             (transpose xs))
 | |
|         ('error)))
 | |
|     (_ 'error)))
 | |
| 
 | |
| (defun getCol (grid x) (getRow (transpose grid) x))
 | |
| 
 | |
| (defun MatrixToList (m) (foldr append nil m))
 | |
| 
 | |
| (defun getSubMatrix (m startcol stopcol startrow stoprow)
 | |
|   (let ((f (lambda (x start stop) (take (drop x start) (- (+ 1 stop) start)))))
 | |
|    (map (lambda (x) (f x startcol stopcol)) (f m startrow stoprow))))
 | |
| 
 | |
| 
 | |
| (defun getSubGridAsList (m x)
 | |
|   (if (and (>= x 0) (< x 9))
 | |
|       (let ((sr (* (// x 3) 3))
 | |
|             (er (+ sr 2))
 | |
|             (sc (* (mod x 3) 3))
 | |
|             (ec (+ sc 2)))
 | |
|         (matrixToList (getSubMatrix m sr er sc ec)))
 | |
|       'error))
 | |
| 
 | |
| (define exampleGrid
 | |
|   '(
 | |
|     (1 2 3 4 5 6 7 8 9)
 | |
|     (2 3 4 5 6 7 8 9 1)
 | |
|     (3 4 5 6 7 8 9 1 2)
 | |
|     (4 5 6 7 8 9 1 2 3)
 | |
|     (5 6 7 8 9 1 2 3 4)
 | |
|     (6 7 8 9 1 2 3 4 5)
 | |
|     (7 8 9 1 2 3 4 5 6)
 | |
|     (8 9 1 2 3 4 5 6 7)
 | |
|     (9 1 2 3 4 5 6 7 8)))
 |