190 likes | 322 Views
CaML continued. CS 351 Programming Paradigms Dept. Computer Science NUI Maynooth. 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);;
E N D
CaML continued CS 351 Programming Paradigms Dept. Computer Science NUI Maynooth Mark Hennessy Dept. Computer Science NUI Maynooth
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. Mark Hennessy Dept. Computer Science NUI Maynooth
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? Mark Hennessy Dept. Computer Science NUI Maynooth
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 Mark Hennessy Dept. Computer Science NUI Maynooth
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. Mark Hennessy Dept. Computer Science NUI Maynooth
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? Mark Hennessy Dept. Computer Science NUI Maynooth
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);; Mark Hennessy Dept. Computer Science NUI Maynooth
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] Mark Hennessy Dept. Computer Science NUI Maynooth
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? Mark Hennessy Dept. Computer Science NUI Maynooth
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. Mark Hennessy Dept. Computer Science NUI Maynooth
Currying cont… • let curryplus a = function b -> a + b;; • int -> int -> int = <fun> • I have defined an anonymous inner function to help with the addition. • What is the result of curryplus 6;; • Answer: int -> int = <fun> • What the hell does this mean? • We’ll discuss that in a minute! Mark Hennessy Dept. Computer Science NUI Maynooth
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? Mark Hennessy Dept. Computer Science NUI Maynooth
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! Mark Hennessy Dept. Computer Science NUI Maynooth
CaML Type System • What about let P x = x;; • We get ‘a -> ‘a = <fun> • 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;; ? Mark Hennessy Dept. Computer Science NUI Maynooth
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”;; Mark Hennessy Dept. Computer Science NUI Maynooth
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;; Mark Hennessy Dept. Computer Science NUI Maynooth
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! Mark Hennessy Dept. Computer Science NUI Maynooth
CaML Type System • CaML checks types via the following general rules: • All occurrences of the same identifier (subject to scope rules) must have the same type. • 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. • 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. Mark Hennessy Dept. Computer Science NUI Maynooth
Reasoning about Types • Recall let P x = x;; ‘a -> ‘a = <fun> let Q x y = x;; ‘a -> ‘b -> ‘a = <fun> • What about the type of let R x y z = x z (y z);; (‘a->’b->’c) -> (‘a->’b) -> ‘a -> ‘c = <fun> Mark Hennessy Dept. Computer Science NUI Maynooth