Haskell Intermediate Introduction
Haskell Intermediate is the next course after Haskell Beginner. It adds functors, applicatives, monads, transformers, and a deeper look at laziness.
A function changes one value into another
double takes a number and returns twice that number. The output shows 21 doubled.
double :: Int -> Int
double x = x * 2
main :: IO ()
main = print (double 21)A list holds many values in order
The list holds four numbers in order. The output shows those numbers in that order.
main :: IO ()
main = print [1,2,3,4]Maybe holds a value or nothing
Just holds a value and Nothing holds no value. The output shows both cases.
main :: IO ()
main = do
print (Just 5 :: Maybe Int)
print (Nothing :: Maybe Int)Example
addOne :: Int -> Int
addOne x = x + 1
main :: IO ()
main = do
print (map addOne [1,2,3])
print (addOne 41)
print (Just (addOne 4))Key points
- Intermediate: the next step after Beginner Haskell.
- Functor: a typeclass for types that can be mapped over.
- Applicative: a typeclass for applying wrapped functions to wrapped values.
- Monad: a typeclass for chaining steps that each produce a wrapped value.
- Transformer: a tool that adds one effect to another monad.
- Laziness: Haskell evaluates a value only when it is needed.
In this example, addOne adds one to a number. The first line maps addOne over a list. The second line applies addOne to 41. The third line wraps the result for 4 in Just.
Note
this course builds on Beginner and does not re-teach its topics.
Try it Yourself
Edit the code, then press Run.
Running is simulated Β· Haskell Playground
Exercise
What does map addOne [1,2,3] give when addOne adds one?