Skip to content

Commit 3e542f3

Browse files
authored
Adding a new exercise and solution to Section 5.1 (#83)
* added new exercise and solution * added namespace and made find's predicate more functional * think I got the whitespace
1 parent bcd5752 commit 3e542f3

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

src/Solutions/Functions2.idr

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,25 @@ handleRequest xs (MkRequest (MkCredentials e pw) album) =
6969
else if elem album albums then Success album
7070
else AccessDenied e album
7171

72-
-- 2
72+
--2
73+
74+
namespace Ex2
75+
76+
data Failure : Type where
77+
UnknownUser : Email -> Failure
78+
InvalidPassword : Failure
79+
AccessDenied : Email -> Album -> Failure
80+
81+
handleRequest : DB -> Request -> Either Failure Album
82+
handleRequest db req = case find ((==) req.credentials.email . email) db of
83+
Nothing => Left (UnknownUser req.credentials.email)
84+
Just u2 => case (u2.email == req.credentials.email && u2.password == req.credentials.password) of
85+
False => Left InvalidPassword
86+
True => case elem req.album u2.albums of
87+
False => Left (AccessDenied req.credentials.email req.album)
88+
True => Right req.album
89+
90+
-- 3
7391
data Nucleobase = Adenine | Cytosine | Guanine | Thymine
7492

7593
readBase : Char -> Maybe Nucleobase
@@ -79,7 +97,7 @@ readBase 'G' = Just Guanine
7997
readBase 'T' = Just Thymine
8098
readBase c = Nothing
8199

82-
-- 3
100+
-- 4
83101
traverseList : (a -> Maybe b) -> List a -> Maybe (List b)
84102
traverseList _ [] = Just []
85103
traverseList f (x :: xs) =
@@ -89,14 +107,14 @@ traverseList f (x :: xs) =
89107
Nothing => Nothing
90108
Nothing => Nothing
91109

92-
-- 4
110+
-- 5
93111
DNA : Type
94112
DNA = List Nucleobase
95113

96114
readDNA : String -> Maybe DNA
97115
readDNA = traverseList readBase . unpack
98116

99-
-- 5
117+
-- 6
100118
complement : DNA -> DNA
101119
complement = map comp
102120
where comp : Nucleobase -> Nucleobase

src/Tutorial/Functions2.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,19 @@ available in the *Prelude* (check out its type!).
353353
`handleRequest`. This should allow you to completely get rid
354354
of the `where` block.
355355

356-
2. Define an enumeration type listing the four
356+
2. Refactor `handleRequest` to use `Either`, such that
357+
`handleRequest : DB -> Request -> Either Failure Album`, where
358+
359+
```idris
360+
data Failure : Type where
361+
UnknownUser : Email -> Failure
362+
InvalidPassword : Failure
363+
AccessDenied : Email -> Album -> Failure
364+
```
365+
366+
Hint: You may find nested `case` statements helpful.
367+
368+
3. Define an enumeration type listing the four
357369
[nucleobases](https://en.wikipedia.org/wiki/Nucleobase)
358370
occurring in DNA strands. Define also a type alias
359371
`DNA` for lists of nucleobases.
@@ -363,7 +375,7 @@ available in the *Prelude* (check out its type!).
363375
`'A'`, `'a'`. Note, that this function might fail, so adjust the
364376
result type accordingly.
365377

366-
3. Implement the following function, which tries to convert all
378+
4. Implement the following function, which tries to convert all
367379
values in a list with a function, which might fail. The
368380
result should be a `Just` holding the list of converted
369381
values in unmodified order, if and
@@ -376,11 +388,11 @@ available in the *Prelude* (check out its type!).
376388
You can verify, that the function behaves correctly with
377389
the following test: `traverseList Just [1,2,3] = Just [1,2,3]`.
378390

379-
4. Implement function `readDNA : String -> Maybe DNA`
391+
5. Implement function `readDNA : String -> Maybe DNA`
380392
using the functions and types defined in exercises 2 and 3.
381393
You will also need function `unpack` from the *Prelude*.
382394

383-
5. Implement function `complement : DNA -> DNA` to
395+
6. Implement function `complement : DNA -> DNA` to
384396
calculate the complement of a strand of DNA.
385397

386398
## The Truth about Function Arguments

0 commit comments

Comments
 (0)