-
Types of expresions:
- ['a','b','c'] :: [Char]
- ('a,b'c') :: (Char, Char, Char)
- [(False, '0'), (True, '1')] :: [(Bool, Char)]
- ([False, True], ('0','1')) :: ([Bool], [Char])
- [tail, init, reverse] :: [[a] -> [a]]
-
Examples of prescribed types
bools: [Bool]/[True]nums :: [[Int]]/[[1,2], [3,4]]add :: Int -> Int -> Int -> Int/add w x y z = w + x + y + zcopy :: a -> (a,a)/copy x = (x, x)apply :: (a -> b) -> a -> b/apply f x = f x
-
Types of functions:
- if
second xs = head (tail xs)then:type secondis[a] -> a - if
swap (x,y) = (y,x)then:type swapis(a,b) -> (b,a) - if
pair x y = (x,y)then:type pairisa -> b -> (a,b) - if
double x = x * 2then:type doubleisNum a => a -> a - if
palindrome xs = reverse xs == xsthen:type palindromeisEq a => [a] -> Bool(nb only equatable a is required, no other restrictions - order is index based) - if
twice f x = f (f x)then:type twiceisa -> a -> a -> a. Correction - according to ghci the type is actually(a->a) -> a -> a. It makes sense intuitively, twice is not curriable in the sense that you can just 3 values of typeaand output an a. Todo find a better way to explain this.
- if
-
Function types cannot generally be instances of Eq as function equivalence is logically equivalent to the halting problem