(* lec08.sml *)
(* Another elegant use of "deep" patterns
(even better with 'as' pattern--try it) *)
fun nondecreasing intlist =
case intlist of
[] => true
| x::[] => true
| hd::(next::tl) =>
(hd <= next andalso nondecreasing (next::tl))
(* integer-constant patterns and exploiting
"first-match" for "optimizations" *)
fun product intlist =
case intlist of
[] => 1
| 0::tl => 0
| 1::tl => product tl
| 2::tl => let val x = product tl in x + x end
| hd::tl => hd * product tl
(* Functions are your friends... *)
(* Example 1 *)
val x = 1 ;
fun f y = x + y ;
val fzero = f(0);
val x = 2 ;
val y = 3 ;
val fxy = f (x+y)
val y = 42;
val fxy2 = f(x+y);
(* Example 2 *)
val x = 1 ;
fun f y = let val x = 2 in fn z => x + y + z end;
val x = 3 ;
val g = f 4 ;
val y = 5 ;
val g6 = g 6 ;