63: Getting Haskell Unit 3 (part 2) - JSToElm
All Episodes

63: Getting Haskell Unit 3 (part 2)

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.

Rounding off algebraic data types!


  • So like we talked last week, about Semigroup, now we’re gonna add a little bit to that, requiring an identity element for the type changes a semigroup to a monoid! That’s it
  • Well, it might seem like a little change, but it has a pretty sizable effect.
  • Summarize identity 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.

    • I’ve mentioned it a few times previously.
    • fold is possible because monoid has this identity function.
    • Example. if you want to combine or mconcat a 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 mconcat an 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.
  • Monoid Laws
  • Empty mappend mempty x = x Basically (++) and mempty [ ] for lists
  • Reverse Empty mappend x mempty = x
  • Associativity - not to be confused with commutativity Does not matter the sequence of or grouping of the operation. We learned this in grade school. if you are adding ( a + b ) + c = a + ( b + c ) In functional you will more often see it described as functions gf for gf. f g h (f g) h = f (g h) or (f°g), h( )) = (f(g°h( ))
  • Concat mconcat = foldr mappend mepty and this we get for free when we’ve implemented the first 3!

Parametrized Types

  • 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

Maybe Types

Episode 15, Nov 2017

  • Maybe represents values that might be missing. This is an implementation solution instead of something like null which has caused us all so much pain, time, and effort to check, account for, and look out for.
  • Solving for missing values with types.
  • data Maybe a = Nothing | Just a this 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”

packing up a maybe


Get Programming With Haskell


Published 20 Dec 2018

A show about learning Elm, Functional Programing, and generally leveling up as a JS developer.
JavaScript To Elm on Twitter