Skip to content

Adding a new exercise and solution to Section 5.1#83

Merged
stefan-hoeck merged 3 commits into
stefan-hoeck:mainfrom
Liable5607:adding-exercise-to-5.1
Jul 11, 2025
Merged

Adding a new exercise and solution to Section 5.1#83
stefan-hoeck merged 3 commits into
stefan-hoeck:mainfrom
Liable5607:adding-exercise-to-5.1

Conversation

@Liable5607

@Liable5607 Liable5607 commented Jul 7, 2025

Copy link
Copy Markdown
Contributor

Hi! I was playing around with the server example in Section 5.1 and I think it would make a nice exercise. I refactored handleRequest to use Either. I found it a nice challenge that helped make me more comfortable with parametric types. This pull request:

  1. adds a prompt in the 5.1 exercises
  2. puts the solution in the solutions section.

I did have to append some values with an apostrophe (e.g. handleRequest', UnknownUser') to get it to compile in the same file as the solution to the previous exercise.

Thank you for the tutorial. I'm really enjoying it. For me at least the exercises are the most helpful part.

@stefan-hoeck

Copy link
Copy Markdown
Owner

Thanks for adding this. To avoid having to use primed function names, you could put everything in its own namespace:

namespace Ex2
  data Failure : Type where
    UnknownUser : Email -> Failure
    InvalidPassword : Failure
    AccessDenied : Email -> Album -> Failure

  handleRequest : DB -> Request -> Either Failure Album
  handleRequest db req = case find (\u1 => u1.email == req.credentials.email) db of
    Nothing => Left (UnknownUser req.credentials.email)
    Just u2 => case (u2.email == req.credentials.email && u2.password == req.credentials.password) of
      False => Left InvalidPassword
      True => case elem req.album u2.albums of
        False => Left (AccessDenied req.credentials.email req.album)
        True => Right req.album

@Liable5607

Liable5607 commented Jul 7, 2025

Copy link
Copy Markdown
Contributor Author

Thank you, I didn't think of that. And in hindsight find ((==) req.credentials.email . email) db is probably more "functional" than making a lambda

@Liable5607

Copy link
Copy Markdown
Contributor Author

I wasn't sure how to handle the fact that this branch was created before but submitted after the other pull request. So I manually changed the initMaybe to reflect the current version. that's why that file has changed too

@stefan-hoeck

Copy link
Copy Markdown
Owner

I wasn't sure how to handle the fact that this branch was created before but submitted after the other pull request. So I manually changed the initMaybe to reflect the current version. that's why that file has changed too

Please don't do this. Git is able to deal with this kind of thing automatically. Otherwise, the preferred way to do this would be a rebase. Now, in order not to further complicate things, you can leave things as they currently are. So, once the linting issues are fixed (there seems to be some trailing whitespace), this should be ready for merging.

@stefan-hoeck stefan-hoeck merged commit 3e542f3 into stefan-hoeck:main Jul 11, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants