4 Types
4 Types
Languages
Types and Type classes in Haskell
False True
> 1 + False
ERROR
• Integer
• The main difference is that it's not bounded so it can be used to represent
really big numbers.
[’a’,’b’,’c’,’d’] :: [Char]
• In general:
• [t] is the type of lists with elements of type t.
[False,True,False] :: [Bool]
• The type of the elements is unrestricted. For example, we can have
lists of lists:
[[’a’],[’b’,’c’]] :: [[Char]]
(False,’a’,True) :: (Bool,Char,Bool)
• In general:
• (t1,t2,…,tn) is the type of n-tuples whose ith components have type ti for any i
in 1…n.
(False,True,False) :: (Bool,Bool,Bool)
(’a’,(False,’b’)) :: (Char,(Bool,Char))
(True,[’a’,’b’]) :: (Bool,[Char])
In general
t1 → t2 is the type of functions that map values of type t1 to
values to type t2.
For example:
(+) :: Num a a a a
(==) :: Eq a a a Bool
(<) :: Ord a a a Bool
26
Hints and Tips
27
Haskell Typeclasses
• Eq: equality
• Ord: comparison
• Num: numerical operations
• Show: convert to string
• Read: convert from string
• Testable, Arbitrary: testing.
• Enum: ops on sequentially ordered types
• Bounded: upper and lower values of a type
• Generic programming, reflection, monads, …
• And many more.