Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Hennessy Dept. Computer Science NUI Maynooth 1 CaML continued CS 351 Programming Paradigms Dept. Computer Science NUI Maynooth.

Similar presentations


Presentation on theme: "Mark Hennessy Dept. Computer Science NUI Maynooth 1 CaML continued CS 351 Programming Paradigms Dept. Computer Science NUI Maynooth."— Presentation transcript:

1 Mark Hennessy Dept. Computer Science NUI Maynooth 1 CaML continued CS 351 Programming Paradigms Dept. Computer Science NUI Maynooth

2 Mark Hennessy Dept. Computer Science NUI Maynooth2 Quick Recap Recall to define functions: let add a b = a + b;; let gt a b = if a >. b then a else b;; let rec addr a b = if b == 0 then 0 else a + addr a (b-1);; All of the functions written to date have been basic functions that don’t really harness the power of FP! Lets talk about, Lists, Higher Order Functions, Currying and Types.

3 Mark Hennessy Dept. Computer Science NUI Maynooth3 Higher Order Functions A function is said to be a higher order function ( or functional form ) if it takes a function as an argument or returns a function as a result. Eg. let rec addr a b c = a b c;; I want addr to perform identically to addr on the previous slide! What do I do?

4 Mark Hennessy Dept. Computer Science NUI Maynooth4 Lists The builtin function map allows us to apply a function a list. Lists are very useful data structures in FP. To declare a list use the [ and ] E.g let l = [1;2;3;4];; Declares a list of 4 ints

5 Mark Hennessy Dept. Computer Science NUI Maynooth5 Lists cont… To append to a list use the @ operator: let l = [1;2] @ [3;4];; To check for an empty list you can check to see if the list is == [] or you can use list_length: list_length [1;] = 1 Iterating over a list gives us a powerful way of manipulating data.

6 Mark Hennessy Dept. Computer Science NUI Maynooth6 Lists cont… To return the value of the first element in the list use hd hd [1;2;3;4];; gives 1 To return the tail of the list ( everything but the head ) use tl tl [1;2;3;4];; gives [2;3;4] Every list is actually a ‘perfect’ list ie there is an implied [] at the end of the list. What does tl [1;] return?

7 Mark Hennessy Dept. Computer Science NUI Maynooth7 Iterating over a List It is quite easy let rec iter list = if list == [] then (* do something/exit *) else (* use the head *) (hd list); iter (tl list);;

8 Mark Hennessy Dept. Computer Science NUI Maynooth8 Higher Order Functions and Lists We can write a function and apply it to every element in a list quite easily: let plusone x = x+1;; map plusone [1;2;3;4];; What is the result? [2;3;4;5]

9 Mark Hennessy Dept. Computer Science NUI Maynooth9 Currying Named after the logician Haskell Curry. This is the process whereby a function that takes many arguments is replaced by a function that expects a single argument and returns a function that expects the remaining arguments! Confused?

10 Mark Hennessy Dept. Computer Science NUI Maynooth10 Currying Example let myadd a b = a+b;; let inc = myadd 3;; inc 4;; What is the expected answer? 7! With currying, all functions “really” only take exactly one argument.

11 Mark Hennessy Dept. Computer Science NUI Maynooth11 Currying cont… let curryplus a = function b -> a + b;; int -> int -> int = I have defined an anonymous inner function to help with the addition. What is the result of curryplus 6;; Answer: int -> int = What the hell does this mean? We’ll discuss that in a minute!

12 Mark Hennessy Dept. Computer Science NUI Maynooth12 Currying cont… Currying allows the ability to partially apply a function! curryplus 6 on its own cannot obviously add 6 to anything but it is not an error to call curryplus. What is the result of curryplus 6 (sub 10 4);; Now, if I do this let curryplus6 = curryplus 6;; curryplus6 (curryplus 6 (sub 10 4));; Any guesses for the answer?

13 Mark Hennessy Dept. Computer Science NUI Maynooth13 CaML Type System let triadd(a,b,c) = a+b+c;; int * int * int -> int let add3 a b c = a+b+c;; int -> int -> int -> int The first takes a triple and returns an int, the second takes 3 ints and returns an int. So far so easy!

14 Mark Hennessy Dept. Computer Science NUI Maynooth14 CaML Type System What about let P x = x;; We get ‘a -> ‘a = What this means is that we take in some unknown type x, which is represented by ‘a and we return the exact same unknown type. What about let Q x = y;; ?

15 Mark Hennessy Dept. Computer Science NUI Maynooth15 CaML Type System CaML therefore allows a certain flexibility with types. As mentioned before we can use polymorphism with some of the inbuilt operators. In CaML, the = operator is polymorphic, it will work for many types. Consider: let Q x y = if x = y then “True” else “False”;; Q 4 5;; Q 5 5;; Q 4.0 4.0;; Q “CS351” “crap”;;

16 Mark Hennessy Dept. Computer Science NUI Maynooth16 CaML Type System CaML can figure out the types for most expressions. let prog a b = if a > b then a(* Line 1 *) else “Not Greater”;;(* Line 2 *) Anything wrong with this program? No! The > operator is polymorphic for a and b on L1 The else part on L2 says the return type is string. What is the function header? What happens with prog 4 5;;

17 Mark Hennessy Dept. Computer Science NUI Maynooth17 CaML Type System The previous example shows how CaML infers the types in an expression. It also checks for type consistency. Consider: let prog a b = if a > b then 1 else “Not Equal” Here we have an error! The two returns are inconsistent being of different types, hence we get errors reported!

18 Mark Hennessy Dept. Computer Science NUI Maynooth18 CaML Type System CaML checks types via the following general rules: 1. All occurrences of the same identifier (subject to scope rules) must have the same type. 2. A programmer-defined function has type ’a -> ’b, where ’a is the type of the function’s parameter and ’b is the type of its result. 3. When a function is called, the type of the argument that is passed must be the same as the type of the parameter in the function’s definition. The result type is the same as the type of the result in the function’s definition.

19 Mark Hennessy Dept. Computer Science NUI Maynooth19 Reasoning about Types Recall let P x = x;; ‘a -> ‘a = let Q x y = x;; ‘a -> ‘b -> ‘a = What about the type of let R x y z = x z (y z);; (‘a->’b->’c) -> (‘a->’b) -> ‘a -> ‘c =


Download ppt "Mark Hennessy Dept. Computer Science NUI Maynooth 1 CaML continued CS 351 Programming Paradigms Dept. Computer Science NUI Maynooth."

Similar presentations


Ads by Google