We've tackled Semigroups, and with a little more effort we can better understand Monoids too. With all this momentum we'll cover parameterized types and the Maybe type too.
x <> id = x as well as
id <> x = x > function that always returns the same value that was used as its argument. In equations, the function is given by f(x) = x.
mconcata list of lists. Like say a list of Strings, which we know under the covers a String is a List of Char
[Char]what if the list is empty ? Do we have to check for that possibility and handle that? That doesn’t seem very helpful. If you try to
mconcatan Empty List, bc of the Identity function, f(x) = x, you’ll just get back an empty list! It totally knows what to do. And the best part is, we don’t have to be constantly checking for this chance or that, the same function can handle and return a wider possibility of values.
mappend mempty x = xBasically (++) and mempty [ ] for lists
mappend x mempty = x
( a + b ) + c = a + ( b + c )In functional you will more often see it described as functions gf for g ∘ f. f g h
(f g) h = f (g h)or (f°g), h( )) = (f(g°h( ))
mconcat = foldr mappend meptyand this we get for free when we’ve implemented the first 3!
Types defined using parameters are called parameterized types (lol) You can think of like generics if you’ve used those. I vaguely remember, was it in Swift, that the cry was if only we had generics !
they allow you to define generic data structures that work with a wide range of existing data
For what I just said, we’ve been using this almost the whole time, we use parameterized types to define and constrain the types of values a Container type can take. Like … List of type String. That is a parametrized typed. It’s a type that will only except types String, or Int, or List of List, whatever we restrict as the type.
Makes a simple example of Type Box
data Box a = Box a deriving show
We make a wrap function, and an unwrap function
Types can have more than one typed parameter ! Like a tuple!
“The type of a type is called a kind” -> wha? this gets into Functor, Applicative, and Monad a bit later
nullwhich has caused us all so much pain, time, and effort to check, account for, and look out for.
data Maybe a = Nothing | Just athis is similar to the box example earlier. Maybe can be thought of as a sort of box that you have to metaphorically “wrap” and “unwrap”