Haskell Cheat Sheet
Haskell Cheat Sheet
0
What is a Type?
Bool
False True
1
Type Errors
> 1 + False
Error
e :: t
3
All type errors are found at compile time, which
makes programs safer and faster by removing
the need for type checks at run time.
[False,True,False] :: [Bool]
[’a’,’b’,’c’,’d’] :: [Char]
In general:
6
Note:
[False,True] :: [Bool]
[False,True,False] :: [Bool]
[[’a’],[’b’,’c’]] :: [[Char]]
7
Tuple Types
(False,True) :: (Bool,Bool)
(False,’a’,True) :: (Bool,Char,Bool)
In general:
(False,True) :: (Bool,Bool)
(False,True,False) :: (Bool,Bool,Bool)
(’a’,(False,’b’)) :: (Char,(Bool,Char))
(True,[’a’,’b’]) :: (Bool,[Char])
9
Function Types
In general:
For example:
mult x y z
Means ((mult x) y) z.
18
Note:
fst :: (a,b) a
head :: [a] a
id :: a a
20
Overloaded Functions
For example:
(+) :: Num a a a a
(==) :: Eq a a a Bool
(<) :: Ord a a a Bool
23
Hints and Tips
24
Exercises
[’a’,’b’,’c’]
(’a’,’b’,’c’)
[(False,’0’),(True,’1’)]
([False,True],[’0’,’1’])
[tail,init,reverse]
25
(2) What are the types of the following functions?
pair x y = (x,y)
double x = x*2
palindrome xs = reverse xs == xs
twice f x = f (f x)