3.5 selectors, discriminators, deconstructors
Going back to the Pair
and Either
type we used in 3.3 types, we need some way to access the first/second element:
first :: Pair a b -> a
first (Pair x y) = x -- Diff variables since a/b are types and x/y are variables referring to values
second :: Pair a b -> b
second (Pair x y) -> y
left :: Either a b -> a
left (Left x) = x
right :: Either a b -> b
right (Right y) = y
This is very natural, similar to the c++ syntax but translated over into haskell. These are called selectors. However, we probably want discriminators to make sure we're using left and right correctly:
isLeft :: Either a b -> Bool
isLeft (Left x) = True
isLeft (Right x) = False
These fall under the general class of types called deconstructors, though they're also called destructors as well. We use these to write functions, and can be nonunique.