-
-
Save Ap0c/84a61f4e7f2d288652326aa4ecdb3f9a to your computer and use it in GitHub Desktop.
Implementation of sudoku in Haskell, from the FP reading group.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Row a = [a] | |
type Matrix a = [Row a] | |
type Digit = Int | |
type Grid = Matrix Digit | |
digits = [1..9] | |
sudoku :: Grid -> [Grid] | |
sudoku = (filter valid) . expand | |
valid :: Grid -> Bool | |
valid g = a && b && c | |
groupValid :: Row Digit -> Bool | |
rows :: Grid -> [Row Digit] | |
rows = id | |
columns :: Grid -> [Row Digit] | |
columns [r] = [ [d] | d <- r ] | |
columns (r:rs) = map prepend zipped | |
where zipped = r `zip` columns rs | |
prepend (d, ds) = d:ds | |
boxes :: Grid -> [Row Digit] | |
boxes = . map groupsOfThree | |
groupsOfThree :: Row a -> [[a]] | |
groupsOfThree [] = [] | |
groupsOfThree r = take 3 r : groupsOfThree (drop 3 r) | |
expand :: Grid -> [Grid] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have forked this gist and made a few amendments for clarity.