61: Getting Haskell Unit 2 (TypeClass) - JSToElm
All Episodes

61: Getting Haskell Unit 2 (TypeClass)

Things are starting to click into place slowly. I am feeling more confident in reading and thinking type signatures and composability functions. Best of all, I am really enjoying it.

Follow up

  • Elm does not currently support type classes.

    • Does it need to?

Using TypeClasses

  • Implement your own type classes
  • Understand polymorphism in Haskell
  • `Know when to use deriving
  • Search for documentation with Hackage and Hoogle

Show

class Show a where
  show :: a -> String
  • turns any value into a string for output
  • So the output we see from GHCI
  • automatically derived typeclass Vanillia | Chocolate

Ord

class Eq a => Ord a where
  compare :: a -> a -> Ordering
  (<) :: a -> a -> Bool
  (<=) :: a -> a -> Bool
  (>) :: a -> a -> Bool
  (>=) :: a -> a -> Bool
  max :: a -> a -> a
  min :: a -> a -> a
  • Cool, Ord is defined as a Eq class. oh that’s rad
  • well i guess if you’re gonna order something you’ll need to compare it’s equally, just like it says in the the compare definition.
  • compare :: Ord a => a -> a -> Ordering
  • man it makes so much sense it’s a bit frightening.

EQ

class Eq a where
  (==) :: a -> a -> Bool
  (/=) :: a -> a -> Bool
  • pretty straight forward takes, 2 values and returns a bool, True or False

Bounded

class Bounded a where
  minBound :: a
  maxBound :: a
  • no functions just values.
  • members of bounded must provide a way to get their Upper and Minimum limit. Char and Int are bounded
  • Now on to programming with Types, Things are getting pretty good.

    • Creating types with and | or
    • Semigroups
    • Parameterized Types
    • The Maybe type

TypeClass

The podcast space

Resources

Get Programming With Haskell

Follow

Published 6 Dec 2018

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