Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 COMP313A Programming Languages Functional Programming (7)

Similar presentations


Presentation on theme: "1 COMP313A Programming Languages Functional Programming (7)"— Presentation transcript:

1 1 COMP313A Programming Languages Functional Programming (7)

2 2 Lecture Outline Type checking –type classes Some Haskell tricks

3 3 Polymorphism versus Operator Overloading Use the same function name with different types Polymorphism – the same function definition Operator overloading – different function for each different type

4 4 Polymorphism first (x, y) = x list x = [x]

5 5 Operator Overloading == Defined for Int and …. One for pairs (n,m) == (p,q) = (n==p) && (m==q)

6 6 Type Classes in Haskell Underpinned by the notion of overloading Mechanism by which overloaded functions can be given types – why? Elem does not make sense for every possible a How can we still have the flexibility of polymorphic types and ensure type safety Use type classes to do this elem :: a -> [a] -> Bool elem x [] = False elem x (y:ys) = (x == y) || elem x ys

7 7 elemBool :: Bool -> [Bool] -> Bool elem x [] = False elem x (y:ys) = (x == Bool y) || elemBool x ys elemInt :: Int -> [Int] -> Bool elem x [] = False elem x (y:ys) = (x == Int y) || elemInt x ys We could have: YUK!

8 8 Type Classes in Haskell Operator Overloading Could instead make the equality function a parameter: But this is too general Instead Where type a is restricted to those types which can be used with equality elem :: (a ->a->Bool) ->a -> [a] -> Bool elem :: a -> [a] -> Bool

9 9 elemBool :: Bool -> [Bool] -> Bool elem x [] = False elem x (y:ys) = (x == y) || elemBool x ys elemInt :: Int -> [Int] -> Bool elem x [] = False elem x (y:ys) = (x == y) || elemInt x ys elem :: Eq a  a -> [a] -> Bool elem x [] = False elem x (y:ys) = (x == y) || elem x ys context

10 10 Operator Overloading Think of as: –the equality operator has a set of types defined for it. These are the types which can be used with the equality operator Thus we say: –The definition of elem can be used over all types with equality Type classes are used to give a type to functions like elem.

11 11 Type Checking and Classes member [] y = False member (x:xs) y = x == y || member xs y Apply the function member to an expression e, whose type is Ord b => [[b]] Eg. A list of lists of integers. Integers have an ordering partial application member e Unify the type expressions giving (without contexts) member :: [[b]] -> [b] -> Bool e::[[b]] The type will be? And member e the type [b] -> Bool

12 12 Type Checking and Classes But also have to unify the contexts (Eq [b], Ord b) i.e are the set of defined for Eq the same as the set of types defined for Ord Now check and simplify the context –the requirements in a context, ([b], b) can only apply to type variables –need to simplify requirements like Eq [b] [3, 5, 6] == [3, 5, 6] 3==3 & 5==5 & 6 == 6 instance Eq a => Eq [a] where…. – (Eq b, Ord b)

13 13 (Eq b, Ord b) Class Eq a => Ord a where …. tells us how the class is derived but not the instances Any instance of Ord is an instance of Eq but … so…. What should the unified context be…

14 14 Ord b member e :: Ord b => [b] -> Bool

15 15 Assignment hints Some Haskell tricks

16 16 the cons operator x:rest x:y:rest x:y:z:rest head x head y head z head (drop 1 x) [x,y,z]:rest To get the last ement in a list use “last” To get a list with all but the first element use “tail” tail rest head last rest [["auckland", "pokeno", "sh1"],["pokeno", "tauranga", "sh2"], ["tauranga", "opotiki", "sh2"], ["opotiki", "gisborne", "sh2"], ["gisborne", "napier", "sh2"], [ "napier", "woodville", "sh2"]]

17 17 How would you get the second last element in a list?

18 18 To make a list x:rest [x] [x,y,z] etc If x y and z are characters then the type of [x,y,z] is String [x,y,z]:rest [x,y,z] == “fred” A function to make a list out of some argument list x = [x]

19 19 Sometimes it is easier to just create an auxiliary function which accumulates the result as an argument Sometimes it is easier to build a list in reverse order Take the list “the quick brown fox” process_list text = process_list_aux text “” process_list_aux [] result = ------------ process_list_aux x:text result = process_list_aux text ----------- : versus ++

20 20


Download ppt "1 COMP313A Programming Languages Functional Programming (7)"

Similar presentations


Ads by Google