Skip to content

Commit 65cd2ef

Browse files
committed
Updated homework
1 parent a386cef commit 65cd2ef

File tree

2 files changed

+15
-73
lines changed

2 files changed

+15
-73
lines changed

Homework/Homework15/app/Homework15A.hs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
module Main where
22

3-
import Control.Exception (catch, try)
4-
import Text.Read (readMaybe)
5-
63
--------------------------------------------------------------------------------
74
--------------------------------------------------------------------------------
85
-- IMPORTANT: Read the README.md file before completing the homework.
@@ -29,31 +26,19 @@ prompt todos = do
2926
command <- getLine
3027
interpretCommand command todos
3128

32-
delete :: Maybe Int -> [a] -> [a]
33-
delete Nothing as = as
29+
delete :: Int -> [a] -> [a]
30+
delete 0 (_ : as) = as
3431
delete _ [] = []
35-
delete (Just 0) (_ : as) = as
36-
delete (Just n) (a : as) = a : delete (Just (n - 1)) as
32+
delete n (a : as) = a : delete (n - 1) as
3733

3834
interpretCommand :: String -> [String] -> IO ()
3935
interpretCommand cmd todos = case cmd of
4036
"q" -> return ()
4137
('+' : ' ' : todo) -> prompt (todo : todos)
42-
('-' : ' ' : num) -> prompt $ delete (readMaybe num) todos
38+
('-' : ' ' : num) -> prompt $ delete (read num) todos
4339
('s' : ' ' : fn) ->
44-
writeFile fn (show todos) `catch` \e -> do
45-
putStrLn $ "Could not write to file because: " ++ show (e :: IOError)
46-
prompt todos
47-
-- There's no reason as to why this and the previous case should handle
48-
-- errors differently. I did it this way so you can see how to handle errors
49-
-- in different ways.
50-
('l' : ' ' : fn) -> do
51-
contents <- try $ readFile fn >>= return . read
52-
case contents of
53-
Left e -> do
54-
putStrLn $ "Could not read from file because: " ++ show (e :: IOError)
55-
prompt todos
56-
Right todos' -> prompt todos'
40+
writeFile fn (show todos)
41+
('l' : ' ' : fn) -> readFile fn >>= prompt . read
5742
_ -> do
5843
putStrLn ("Invalid command: `" ++ cmd ++ "`")
5944
prompt todos

Homework/Homework15/src/Homework15B.hs

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
module Homework15B where
44

5-
import Control.Exception (IOException, try)
6-
75
--------------------------------------------------------------------------------
86
--------------------------------------------------------------------------------
97
-- IMPORTANT: Read the README.md file before completing the homework.
@@ -14,26 +12,19 @@ import Control.Exception (IOException, try)
1412
-- If the list is empty, return Nothing.
1513

1614
headMaybe :: [a] -> Maybe a
17-
headMaybe [] = Nothing
18-
headMaybe (x : _) = Just x
15+
headMaybe = undefined
1916

2017
-- 2. Write a function that takes a list of Maybe values and returns a list of all the Just values.
2118
-- If there are no Just values, return an empty list.
2219

2320
catMaybes :: [Maybe a] -> [a]
24-
catMaybes [] = []
25-
catMaybes (Nothing : xs) = catMaybes xs
26-
catMaybes (Just x : xs) = x : catMaybes xs
21+
catMaybes = undefined
2722

2823
-- 3. Write a function that tries to read from a file and returns the contents of the file.
2924
-- If the file does not exist, return Nothing.
3025

3126
readFileMaybe :: FilePath -> IO (Maybe String)
32-
readFileMaybe path = do
33-
contents <- try $ readFile path
34-
case contents of
35-
Left (_ :: IOException) -> return Nothing
36-
Right c -> return (Just c)
27+
readFileMaybe = undefined
3728

3829
-- 4. Write a function that checks all the requirements for a password using the
3930
-- Either type with a custom data type for errors.
@@ -43,53 +34,19 @@ readFileMaybe path = do
4334
-- - The password must contain at least one uppercase letter.
4435
-- - The password must contain at least one lowercase letter.
4536

46-
data PasswordError
47-
= NotLongEnough
48-
| NoDigit
49-
| NoUppercase
50-
| NoLowercase
51-
deriving (Eq, Show)
37+
data PasswordError = WrongConstructor
5238

5339
passwordLongEnough :: String -> Either PasswordError String
54-
passwordLongEnough password =
55-
if length password >= 10
56-
then Right password
57-
else Left NotLongEnough
40+
passwordLongEnough = undefined
5841

5942
passwordHasDigit :: String -> Either PasswordError String
60-
passwordHasDigit password =
61-
if any (`elem` ['0' .. '9']) password
62-
then Right password
63-
else Left NoDigit
43+
passwordHasDigit = undefined
6444

6545
passwordHasUppercase :: String -> Either PasswordError String
66-
passwordHasUppercase password =
67-
if any (`elem` ['A' .. 'Z']) password
68-
then Right password
69-
else Left NoUppercase
46+
passwordHasUppercase = undefined
7047

7148
passwordHasLowercase :: String -> Either PasswordError String
72-
passwordHasLowercase password =
73-
if any (`elem` ['a' .. 'z']) password
74-
then Right password
75-
else Left NoLowercase
76-
77-
--------------------------------------------------------------------------------
78-
--------------------------------------------------------------------------------
79-
-- As you can see, the passwordRequirements function is very repetitive.
80-
-- This is one of the downsides of using nested optional values.
81-
-- We're going to solve this problem in the "Basic Abstractions" section of the coruse.
82-
--------------------------------------------------------------------------------
83-
--------------------------------------------------------------------------------
49+
passwordHasLowercase = undefined
8450

8551
passwordRequirements :: String -> Either PasswordError String
86-
passwordRequirements password =
87-
case passwordLongEnough password of
88-
Left err -> Left err
89-
Right _ -> case passwordHasDigit password of
90-
Left err -> Left err
91-
Right _ -> case passwordHasUppercase password of
92-
Left err -> Left err
93-
Right _ -> case passwordHasLowercase password of
94-
Left err -> Left err
95-
Right _ -> Right password
52+
passwordRequirements = undefined

0 commit comments

Comments
 (0)