[Haskell Cheat Sheets] (https://gist.github.com/vpayno/0a7db3ff9a4fac8774c5)
Haskell has a type interface which allows it to infer what the types are.
To examine the type of an expression use the :t command in GHCI.
Prelude> :t 'a'
'a' :: Char
Prelude> :t 1
1 :: Num a => a
Prelude> :t 1.1
1.1 :: Fractional a => a
Prelude> :t "Hi"
"Hi" :: [Char]
Prelude> :t True
True :: Bool
Prelude> :t length [1,2,3]
length [1,2,3] :: Int
Prelude> :t [1,2,3]
[1,2,3] :: Num t => [t]
Prelude> :t (1, "two")
(1, "two") :: Num t => (t, [Char])
-
If a type is Num it can act as an Int, Integer, Float, or Decimal.
-
If a type is Integral it can act as an Int or Integer.
-
If a type is Floating it can act as a Float or Decimal.
-
The fromIntegral function takes an Int or Integer and makes it a Num so it can play nice with floating point types. For some weird reason, length returns an Int instead of a Num. You can "fix" that with fromIntegeral.
fromIntegral (length [1,2,3]) + 4.7
-
The String type is an alias to the [Char] type and all Char arrays are treated as Strings.
-
In a function type declaration the last item in the -> chain is the return type.
-
To define the function type declaration when using types:
double :: Integer -> Integer
double n = n * 2
:t double
> double :: Integer -> Integer
- To define the function type declaration when using typeclasses:
double :: Num n => n -> n
double n = n * 2
:t double
> double :: Num n => n -> n
``
- **Int** is bounded but **Integer** is not.
minBound :: Int
-9223372036854775808
maxBound :: Int
9223372036854775807
- **Bool** is bounded.
minBound :: Bool
False
maxBound :: Bool
True
- **Char** is bounded.
minBound :: Char
'\NUL'
maxBound :: Char
'\1114111'
- Tuples are bounded if its elements are also bounded.
maxBound :: (Bool, Int, Char)
(True,9223372036854775807,'\1114111')
- Ordered types
- Enum: (), Bool, Char, Odering, Int, Integer, Float, and Double
- can be enumerated
- have successors and predecessors (succ & pred)
- Show typeclass can be presented as strings. The **show** function takes a value and turns it into a string.
show 123
"123"
show 1.23
"1.23"
show True
"True"
- Read typeclass is the opposite of the show typeclass. The **read** function takes a string and converts it to a member of Read. It needs a context to figure out what to convert the string to.
read "True" || False
True
read "False" || False
False
read "1.23" + 4.32
5.550000000000001
read "3" + 7
10
read "[1,2,3]" ++ [3]
[1,2,3,3]
- Operator (infix functions) types:
:t (+) (+) :: Num a => a -> a -> a
:t (-) (-) :: Num a => a -> a -> a
:t () () :: Num a => a -> a -> a
:t (/)
(/) :: Fractional a => a -> a -> a
:t (^)
(^) :: (Integral b, Num a) => a -> b -> a
:t (^^)
(^^) :: (Fractional a, Integral b) => a -> b -> a
:t (**)
(**) :: Floating a => a -> a -> a
:t (==)
(==) :: Eq a => a -> a -> Bool
:t (/=)
(/=) :: Eq a => a -> a -> Bool
:t (>)
(>) :: Ord a => a -> a -> Bool
:t (<)
(<) :: Ord a => a -> a -> Bool
:t (>=)
(>=) :: Ord a => a -> a -> Bool
:t (<=)
(<=) :: Ord a => a -> a -> Bool
### Type variables ###
Type variables are like generics. They are used in polymorphic functions.
```haskell
:t head
> head :: [a] -> a
:t fst
> fst :: (a, b) -> a