(* Programming Languages (F28PL) lecture transcript - 5th October at 15:15 *) (* Creating value x which refers to 5 *) val x = ref 5; x := 6; (* returns a unit *) (x := 6, x := 7); (* returns a tuple of units *) val y = 6; (* y is immutable *) val s = [y]; (* returns [6] *) val y = 7; s; (* still returns [6] *) val s = [x]; (* s = [ref 7] *) x := 4; s; (* returns [ref 4] *) val y = ref 4; val s = [x,x]; val t = [x,y]; (s, t); y := 100; (* Dereferencing x *) !x; val z = 5; !z; (* Will give a type error *) val s = [ref 5, ref 5, ref 5]; hd (tl s) := 7; s; (* The two operatations *) op :=; op !; fun firstof l = (map hd) l; (* Another way to write the function *) fun firstof [] = [] | firstof (h::t) = (hd h)::(firstof t); (* Also another way to write it *) fun firstof [] = [] | firstof ((hh::tt)::t) = hh::(firstof t); val countdown = [["one"], ["two", "one"], ["three", "two", "one"]]; firstof countdown; map; hd; hd []; (* raises an error *) (* Reverse function *) fun allrev l = (map rev) l; allrev countdown; fun lastof l = (firstof o allrev) l; lastof countdown;