(* Programming Languages (F28PL) lecture transcript - 14th September at 15:15 *) (* The addition function is a datum which can be passed to other functions *) op +; (* A function which runs function f twice on x *) fun dotwice f x = f(f(x)); (* A function with a type int -> int (inputs an integer, outputs an integer) *) fun addone x = x+1; (* Returns a function int -> int, adding 1 twice *) dotwice addone; (* Returns 3, as the function added 1+1+1 *) it 1; (* This is an int *) 5; (* This is a real *) 5.0; (* We have to explicity cast, so you cannot add real to int *) 0 + 0.0; (* This function casts int to real *) real; real(0) + 0.0; (* true has type bool *) true; (* The following computations will return a bool *) true = true; 1 = 1; (* And we can use these tests in if statements *) if true then 5 else 6; (* returns 5, as true is always true *) if (5 = 6) then 5 else 6; (* returns 6, as 5 is not 6 *) (* This creates a local variable x which is bound to 6 *) val x = 6; val x = 7; (* Value x = 6 is still there, but cannot be accessed in the current scope *) x; (* returns 7 *) (* The variables x and y are only in the scope of the function (x + y) *) fun add x y = x + y; (* We tell the interpreter that x must be of type bool, <> is inequality sign *) fun xor (x:bool) y = (x <> y); xor true false; xor false false; (* A vulgar negation function -- don't do this *) fun myvulgarnotfunction x = if x=true then false else true; (* The proper alternative to the function above *) fun myelegantfunction x = (x=false); (* The equals operator *) op =; (* An empty list *) []; (* A list with 0 *) [0]; (* A list with strings *) ["hello","world"]; (* A function which doubles the elements of a list *) fun doublelist x = x @ x; (* List concatenation operator *) op @; doublelist ["allo"]; (* Function which returns sum of the elements of a list *) fun sumlist [] = 0 | sumlist (hd::tl) = hd + (sumlist tl); sumlist [5,5,5]; sumlist [5, "five"]; (* will give us type error *)