The Validation Course
The course begins with two lessons on
case expressions to ensure a solid foundation. From there, we write three functions for checking that inputs are valid passwords according to the rules of our system. The rest of the lessons iteratively expand on and refactor those functions into a small program that validates usernames and passwords, constructs a
User if both are valid inputs, and returns pretty error messages if they are not. Along the way we learn about
Applicative and how they are similar and different and how to use types to rethink our solutions to problems.
The first lesson takes a look at conditional
if-then-elseexpressions in Haskell and compares them to a similar pattern with different syntax:
This lesson works through an example to see how
caseexpressions allow us to branch, like conditionals, on values other than booleans – specifically, working with
Maybeto distinguish different kinds of false-ness.
In this lesson, we begin writing the code that will be extended and refactored throughout the rest of the course. We write three functions that check
Stringinputs against length and character requirements and check for and strip off leading whitespace.
The last lesson left you with an exercise to do. This lesson leads off with two solutions to that exercise and introduces an operator,
>>=, that can reduce nested
Maybeinto a concise composition.
We change from using
Maybeto using another sum type called
Eitherto see if that helps solve some of the issues we have noticed in our code so far.
Our code is now looking pretty good, but we want to extend it to include validation of user name inputs as well as to construct a
Userthat is a product of a username and a password. We also introduce
newtypes here so that we can distinguish between passwords, errors, and usernames at the type level.
In this lesson we find that using
newtypes introduces a need to alter some of our expressions, and we will use operators from the
Applicativetypeclass to help us with that.
This lesson introduces the
validationlibrary and the
Validationis very similar to
Either, the differences force us to change our code significantly, as
Validationcannot be a
Monad. We’ll talk about why not and step through all the ramifications for our code.
We are now accumulating and returning informative error messages about which inputs are invalid and why, but this lesson introduces a couple extra steps to tidy them up for presentation to our users.
This course assumes very little prior knowledge of Haskell. We are focused here on working through examples without understanding theory or how and why things work too deeply. This course has goals and an approach similar to Julie’s famous blog postThe Nesting Instinct.
We do use recursion in one function without explaining it fully. We also rely on and derive instances for several typeclasses without explaining what typeclasses are or how they work; how they work is briefly discussed during lesson 8 when we need to write an instance of
- Instructor: Julie Moronuki
- Window manager: xmonad
- Text editor: Atom with the
- Font: Ubuntu Mono (editor and terminal)
- Syntax theme: Ficus Light in Atom; terminal has a custom color scheme based off Ficus DarkFind out more about the Ficus themes here!
- GHCi note: My GHCi configuration has
:set +ton all the time. The
+toption tells you the type of an expression you enter at the prompt. It is what displays the type of
itafter I evaluate an expression as in the third line here:
That is not something GHCi does by default, so if yours doesn’t do it, don’t worry about it. You can turn this on in your GHCi if you want by entering
:set +t at the prompt, but it won’t affect anything we’re doing in this course.
There are also some language extensions I keep turned on in my GHCi config most of the time; you’ll learn about one of these,
-XTypeApplications, in lesson 4.