(* Programming Languages (F28PL) lecture transcript - 13th September at 15:15 *) (* Checking the type of the + operator *) op +; (* Defining a function which inputs x and returns x ; has type 'a -> 'a *) fun identity x = x; (* This would return 5 *) identity 5; (* Since it has polymorphic type, we can pass anything to it *) identity "Hello world"; (* Power function in the built-in library *) Math.pow(2.0, 8.0); (* Let's make our own "tail recursive" power function *) fun pow x 0 = 1 | pow x y = x*(pow x (y-1)); (* This will return 1 as it matches the first case (if x=0, return 1) *) pow 2 0; (* This should return 256 *) pow 2 8; (* Let's make another function, which is not tail recursive *) fun crazy x 0 = x | crazy x y = (crazy x (y-1))*(crazy x (y-1)); (* We can see that a simple task would take longer with the crazy function *) crazy 2 10; (* We can pass the first integer to the pow function *) pow 2; (* Now we can pass another integer to 'it' *) pow 8; (* In this example, f has type int -> 'a *) fun atzero f = f 0; (* Here we specify that f must have type int -> int *) fun atzero f = (f 0):int; (* Now we can use atzero to pass a second integer (0) to pow *) atzero (pow 2); (* We can now make another function, but instead of passing 0, it would pass 8 *) fun ateight f = (f 8):int; (* And we can see the result of 3^8 *) ateight (pow 3); (* Let's make our own plus function *) fun plus x y = x+y; (* Simple, no big deal *) plus 0 4; (* Now let's make another one *) fun otherplus (x,y) = x+y; otherplus (2,3); (* Should return 5, just as *) plus 2 3; (* If we only provide one number with plus we get something sensible *) plus 2; (* otherplus 2; (* however will give us an error *) *) (* A stupid recursive add function *) fun stupidadd x 0 = x | stupidadd x n = 1+(stupidadd x (n-1)); (* We can show the type of two integers *) (5,4); (* We can see for length, it take in a list of alphas and returns an integer *) length; (* 0.0 has type real *) 0.0; (* we cannot add an int and a real; raises a type error *) 0.0+5; (* real can input an integer and output a real *) real; real 5; (* To convert a real to an integer we have to use the floor function *) floor; (* We can chain functions with the o operator, similar to pipelining in bash *) real o floor; (* real -> real *)