Skip to content

Commit 19f41e5

Browse files
committed
Add comments to this weeks code
1 parent 44a59a6 commit 19f41e5

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

ass3/Main.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,35 @@ solveCSP str = [d | d <- domains, and $ map (\x -> evalCmp x d) constraints]
1717
domains = valuations (map parseDomain (split ',' domain))
1818
constraints = map toComparison (split ',' constr)
1919

20+
-- Given a constraint satisfaction problem, returns a 2-tuple containing the
21+
-- intervals in the first member and the constraints in the second member
2022
splitCSP :: String -> (String, String)
2123
splitCSP str = (domain, constr)
2224
where domain = takeWhile (/= '}') (drop ldomain str)
2325
constr = (tail.init) (drop (length domain + ldomain + lconstr) str)
2426
ldomain = length "domains{"
2527
lconstr = length "constraints{"
2628

29+
-- Splits a string delimited by a character into its substrings
2730
split :: Char -> String -> [String]
2831
split _ [] = []
2932
split c (str) = takeWhile (/= c) str : split c rem
3033
where end = (dropWhile (/= c) str)
3134
rem = if null end then [] else tail end
3235

36+
-- Parses a single interval and returns a 2-tuple containing the name of the
37+
-- interval and the size of the interval. Because there are no regexes in
38+
-- Haskell this function is a bit FUBAR.
3339
parseDomain :: String -> (Name, Domain)
3440
parseDomain str = (name, domain)
3541
where name = takeWhile (isAlpha) str
36-
first = read (takeWhile (isDigit) ((drop (length name + lbs)) str)) :: Integer
37-
second = read ((init.tail.tail) (dropWhile (/= '.') str)) :: Integer
42+
first = read (takeWhile (isDigit) ((drop (length name + lbs)) str))
43+
second = read ((init.tail.tail) (dropWhile (/= '.') str))
3844
lbs = length "<-["
3945
domain = [first .. second]
4046

4147
main :: IO()
4248
main = do input <- readInput
49+
putStr "solutions = "
4350
putStrLn (show (solveCSP input))
4451
return ()

0 commit comments

Comments
 (0)