2
2
3
3
module Homework15B where
4
4
5
- import Control.Exception (IOException , try )
6
-
7
5
--------------------------------------------------------------------------------
8
6
--------------------------------------------------------------------------------
9
7
-- IMPORTANT: Read the README.md file before completing the homework.
@@ -14,26 +12,19 @@ import Control.Exception (IOException, try)
14
12
-- If the list is empty, return Nothing.
15
13
16
14
headMaybe :: [a ] -> Maybe a
17
- headMaybe [] = Nothing
18
- headMaybe (x : _) = Just x
15
+ headMaybe = undefined
19
16
20
17
-- 2. Write a function that takes a list of Maybe values and returns a list of all the Just values.
21
18
-- If there are no Just values, return an empty list.
22
19
23
20
catMaybes :: [Maybe a ] -> [a ]
24
- catMaybes [] = []
25
- catMaybes (Nothing : xs) = catMaybes xs
26
- catMaybes (Just x : xs) = x : catMaybes xs
21
+ catMaybes = undefined
27
22
28
23
-- 3. Write a function that tries to read from a file and returns the contents of the file.
29
24
-- If the file does not exist, return Nothing.
30
25
31
26
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
37
28
38
29
-- 4. Write a function that checks all the requirements for a password using the
39
30
-- Either type with a custom data type for errors.
@@ -43,53 +34,19 @@ readFileMaybe path = do
43
34
-- - The password must contain at least one uppercase letter.
44
35
-- - The password must contain at least one lowercase letter.
45
36
46
- data PasswordError
47
- = NotLongEnough
48
- | NoDigit
49
- | NoUppercase
50
- | NoLowercase
51
- deriving (Eq , Show )
37
+ data PasswordError = WrongConstructor
52
38
53
39
passwordLongEnough :: String -> Either PasswordError String
54
- passwordLongEnough password =
55
- if length password >= 10
56
- then Right password
57
- else Left NotLongEnough
40
+ passwordLongEnough = undefined
58
41
59
42
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
64
44
65
45
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
70
47
71
48
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
84
50
85
51
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