3.3 types

Haskell has types! Wow, so amazing. However it's so prevalent an idea it's kind of scary.

A function can be categorized by its type, which is a type of the form type a -> type b. Lists themselves can also be categorized by types, specifically [type data]. n-Tuples are also types, but this is just a product, so it's not really as interesting, except there is the 0-tuple, if that's interesting.

Haskell supports type synonyms/naming with the type keyword:

type Name = [Char] -- If you want to define a name as a sequence of characters

You can override existing types if you want, but existing types are probably typed their own way for a reason. Haskell also let's you do the reasonable thing where you define a type based on the possible constants it can take. This uses the data keyword, which lets you describe constructors:

data Bool = False | True -- Bool can be either False or True

You have to set up the structure on these, as currently they're just constants that are of the type Bool. Because Haskell is kind, we have the soft types Maybe type and Either type a type b:

data Maybe a = Nothing | Just a -- It could be type a! Or nothing.
data Either a b = Left a | Right b -- No idea what the left/right are doing here

The Just constructor sends something of type a to the Maybe type. Same with Either, with Left and Right being the two types of constructors, which you'll use depending on the type of the input, since order matters. Confusingly this allows for Either Int Int, and distinguishes between Either Int Char and Either Char Int, but that's probably fine, since you'll never actually need it to be unordered (hopefully).

Since Pair is a function, you might also want to define a type:

data PairType a b = Pair a b -- Basically (a, b)
Pair :: a -> b -> PairType a b -- Basically what happens above

Note that this function starts with a capital letter (whoop), which is unique. Also, names for types/constructors can be the same thing, since they're fundamentally different operations and therefore can be named the same:

data Pair a b = Pair a b -- It looks stupid, probably because it is, but it's honest and very nice