Programming in Haskell: Chapter 8 - Declaring Types and Classes
Programming in Haskell: Chapter 8 - Declaring Types and Classes
1
Type Declarations
2
Type declarations can be used to make other types
easier to read. For example, given
we can define:
origin :: Pos
origin = (0,0)
we can define:
copy :: a Pair a
copy x = (x,x)
4
Type declarations can be nested:
5
Data Declarations
7
Values of new types can be used in the same ways
as those of built in types. For example, given
we can define:
answers :: [Answer]
answers = [Yes,No,Unknown]
we can define:
10
Not surprisingly, data declarations themselves can
also have parameters. For example, given
we can define:
Zero
Succ Zero
1 + (1 + (1 + 0)) = 3
14
Using recursion, it is easy to define functions that
convert between values of type Nat and Int:
15
Two naturals can be added by converting them to
integers, adding, and then converting back:
Note:
1
2 3
18
Using recursion, a suitable new type to represent
such expressions can be declared by:
19
Using recursion, it is now easy to define functions
that process expressions. For example: