Skip to content

Instantly share code, notes, and snippets.

@Ap0c

Ap0c/sudoku.hs Secret

Created August 9, 2018 14:08
Show Gist options
  • Save Ap0c/84a61f4e7f2d288652326aa4ecdb3f9a to your computer and use it in GitHub Desktop.
Save Ap0c/84a61f4e7f2d288652326aa4ecdb3f9a to your computer and use it in GitHub Desktop.
Implementation of sudoku in Haskell, from the FP reading group.
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]
@regiskuckaertz
Copy link

I have forked this gist and made a few amendments for clarity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment