Type
A collection of related values.
Type Error
Applying a function to arguments with the wrong types.
- Found at compile time
- Safe
- Fast
Type Inference
- Every well-formed expression has a type
- Type can be automatically calculated at compile time
Basic Types
Bool
Char
String -- A list of Char
Int -- Fixed-precision
Integer -- Arbitrary-precision
Float
Other Types
List
- A sequence of values of the same type
- No information about the length
Tuple
- A sequence of values of the different types
- Encodes information about the size
Function
- A mapping from a value to another
Curried Function
- Functions that take arguments one at a time
- Useful functions can be made by partially applying a curried function
- Currying conventions
- Arrow
->
associates to the right - Function applications associate to the left
- Arrow
add' :: Int -> Int -> Int -- Right associative
add' x y = x + y -- add' x = \y -> x + y
-- Compared to
add :: (Int, Int) -> Int
add x y = x + y
Polymorphic Function
- Type contains one or more type variables
- Must begin with a lower-case letter, usually named
a
,b
,c
, etc.
- Must begin with a lower-case letter, usually named
length [a] -> Int
Overloaded Function
- Type contains one or more class constraints
Num
Eq
Ord
Example:
(+) :: Num a => a -> a -> a
(==) :: Eq a => a -> a -> Bool
(<) :: Ord a => a -> a -> Bool