(* Programming Languages (F28PL) lecture transcript - 20th September at 15:15 *) (* Question 3 *) fun isDigit x (x >= #"0" andalso x <= #"9"); isDigit #"0"; (* should return: true *) isDigit #"9"; (* should return: true *) isDigit #")"; (* should return: false *) isDigit #"6"; (* should return: true *) (* NAND function, but this violates the question's guideline *) fun NAND x y = not (x andalso y); (* With pattern matching *) fun NAND true true = false | NAND true false = true | NAND false true = true | NAND false false = true; (* A shorter version, following question's guideline *) fun NAND true true = false | NAND _ _ true; (* A function with matches which are not exhaustive *) fun N true true = false; N true true; (* fine *) N true false; (* throws a dynamic (runtime) error *) (* Provoking a syntax error *) parallelogram; Terminator 2; (* Provoking a type error *) 5 2; (* 5 does not have function type *) fun count 0 = "zero" | count 1 = "one" | count 10000 = "a lot"; count 0; count 1; count ~1; (* Match exception *) (* Using a wildcard to prevent match exception *) fun count 0 = "zero" | count 1 = "one" | count _ = "many"; count ~1; (* returns "many". Is this desired behaviour (matter of opinion)? *) (* Same function, but handles negative numbers differently *) fun count 0 = "zero" | count 1 = "one" | count x = if x > 0 then "positive many" else "negative number"; (* A function which returns the length of 'a list *) fun mylength [] = 0 | mylength (hd::tl) = 1 + (mylength tl); mylength [2,3,5];