(* Programming Languages (F28PL) lecture transcript - 27th September at 11:15 *) (* We know what a function called "not" with the type bool -> bool does *) not; fun mynotfunction x = not x; (* We can bind the function "not" to a value *) val mynotfunction = not; (* 'fun mynotfunction' above is a shorthand of *) val mynotfunction = fun x => not x; (* which is vulgar because it can be replaced to "= not;" *) (* Function which takes an argument of any type, and always returns 2 *) fun futile x = 2; futile 1; futile "f28pl"; futile (fn x => x); (* A function similar to above, but with type 'a -> unit *) fun devnull x = (); (); (* The function we gave as an argument is created and thrown away *) devnull (fn x => x); (* Was the function we created actually executed? *) (* A function which will take time to compute *) fun BC 0 = 10 | BC n = (BC (n-1)) * (BC (n-2)); BC 6; (* This function will never terminate, no matter the value given *) devnull (BC 1); (* Will BC be called? *) -42; (* - is the subtraction operator, so this will not work *) op -; op ~; ~42 (* Negative 42 integer *) (* View functions of Int *) signature S = INTEGER; Int.precision; (* 63 bit integer limit *) Int.maxInt; (* Maximum value of an integer *) (* A tuple of functions *) (op +, op -); (* We have add for ints and reals, which is ad-hoc polymorphism (overloading) *) 5.0 + 5.0; 5 + 5; (* A tuple with different types, underlying representation and properties *) (5, 5.0, "five"); 5*100000000000000000000000000; (* Overflow because it is an integer *) 5.0*100000000000000000000000000.0; (* But it is fine when working with floating point *) (* Concatinating strings *) "hello"^" world"; explode; (* string -> char list *) implode; (* char list -> string *) explode "hello world"; (* rev has type of 'a list -> 'a list *) rev; implode rev explode "hello world"; (* PolyML assumes you are passing the functions as arguments *) implode (rev (explode "hello world")); (implode o rev o explode) "hello world"; (* Infix command to chain functions *)