Skip to content

Commit d909dc8

Browse files
author
matteB10
committed
hej labb4
1 parent 1f24b7c commit d909dc8

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

Sudoku.hs

+4-3
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ isOkay sud = all isOkayBlock $ blocks sud
151151

152152
-- | Positions are pairs (row,column),
153153
-- (0,0) is top left corner, (8,8) is bottom left corner
154-
type Pos = (Int,Int)
155-
156154
-- * E1
157155

158156
blanks :: Sudoku -> [Pos]
@@ -209,6 +207,7 @@ solve' sudoku blank | (not $ isSudoku sudoku) || (not $ isOkay sudoku) = []
209207
| otherwise = filter (\x -> isOkay x && isFilled x) solution
210208
where solution = concat [solve' (update sudoku (head blank) (Just c)) (drop 1 blank) | c <- [1..9]]
211209

210+
212211
-- * F2
213212
-- | Reads a sudoku from a file, solves it and prints the solution if found
214213
readAndSolve :: FilePath -> IO ()
@@ -227,8 +226,10 @@ isSolutionOf sud1 sud2 = isOkay sud1 && isFilled sud1
227226
prop_SolveSound :: Sudoku -> Property
228227
prop_SolveSound sudoku = isSudoku sudoku && isOkay sudoku ==> solveSound (solve sudoku) sudoku
229228
where solveSound :: Maybe Sudoku -> Sudoku -> Bool
230-
solveSound (Just sudoku) sud = isSolutionOf (fromJust $ Just sudoku) sud
229+
solveSound (Just sudoku) sud = isSolutionOf sudoku sud
231230
solveSound Nothing _ = False
232231

233232
-- | Fewer checks that can be used instead of quickChecks standard 100 test's
234233
fewerChecks prop = quickCheckWith stdArgs{maxSuccess=30 } prop
234+
235+

SudokuMB.hs

+24-1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ module Sudoku where
212212
where blank = findIndices (== Nothing) r
213213
iList = replicate (length blank) i
214214

215+
blankRow' :: Row -> Int -> [Pos]
216+
blankRow' [] _ = []
217+
blankRow' (c:cs) i = zip iList blank ++ blankRow rs (i + 1)
218+
where blank = findIndices (== Nothing) r
219+
iList = replicate (length blank) i
220+
215221

216222
-- | Check that all elements in the list of blanks actually is blank
217223
-- by filtering out all 'Nothing' elements and see if something is left
@@ -261,7 +267,24 @@ module Sudoku where
261267
| isFilled sudoku && isOkay sudoku = [sudoku]
262268
| otherwise = filter (\x -> isOkay x && isFilled x) solution
263269
where solution = concat [solve' (update sudoku (head blank) (Just c)) (drop 1 blank) | c <- [1..9]]
264-
270+
271+
272+
-- | extra assignments
273+
solve'' :: Sudoku -> [Pos] -> [Sudoku]
274+
solve'' sudoku blank | (not $ isSudoku sudoku) || (not $ isOkay sudoku) = []
275+
| isFilled sudoku && isOkay sudoku = [sudoku]
276+
| otherwise = filter (\x -> isOkay x && isFilled x) solution
277+
where solution = concat [solve'' (update sudoku pos (Just c)) (drop 1 blank) | c <- [1..9]]
278+
pos = take 1 $ getMax $ map noOfBlanks (rows sudoku)
279+
280+
noOfBlanks :: Row -> Int
281+
noOfBlanks [] = []
282+
noOfBlanks (r:rs) = (length $ blankRow' r):noOfBlanks rs
283+
284+
getMax :: [Int] -> [Int]
285+
getMax [] = []
286+
getMax (x:xs) = if x < (head xs) then getMax xs else getMax (x:tail xs)
287+
265288
-- * F2
266289
-- | Reads a sudoku from a file, solves it and prints the solution if found
267290
readAndSolve :: FilePath -> IO ()

testHaskell.hs

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
11
import Data.List
2+
3+
{-
24
sortFile :: FilePath -> IO ()
35
46
sortFile filename =
57
do input <- readFile filename
68
l <- lines input
79
sortedString <- sort l
8-
writeFile filename sortedString
10+
writeFile filename sortedString
11+
-}
12+
13+
14+
data Expr = Val Int | Div Expr Expr
15+
16+
eval :: Expr -> Maybe Int
17+
eval (Val i) = Just i
18+
eval (Div x y) = cases (eval x) (\n -> cases (eval y) (\m -> safeDiv n m))
19+
20+
eval' :: Expr -> Maybe Int
21+
eval' (Val i) = Just i
22+
eval' (Div x y) = do
23+
n <- eval x
24+
m <- eval y
25+
n `safeDiv` m
26+
27+
-- | Cases used as binding operator >>=
28+
cases :: Maybe Int -> (Int -> Maybe Int) -> Maybe Int
29+
cases m f = case m of
30+
Nothing -> Nothing
31+
Just x -> f x
32+
33+
safeDiv :: Int -> Int -> Maybe Int
34+
safeDiv m n = if n == 0 then Nothing else Just $ div m n

0 commit comments

Comments
 (0)