(* utilities *) fun map f nil = nil | map f (h::t) = (f h)::(map f t); fun fold f id nil = id | fold f id (h::t) = f h (fold f id t); fun filter f nil = nil | filter f (h::t) = if (f h) then (h::(filter f t)) else (filter f t); fun delete x L = filter (fn y => not(x = y)) L; fun remdups nil = nil | remdups (h::t) = h::(remdups (delete h t)); fun member x nil = false | member x (h::t) = (x = h) orelse (member x t); fun subset a b = fold (fn x => fn y => x andalso y) true (map (fn x => member x b) a); fun seteq a b = (subset a b) andalso (subset b a); (* find nth element of a set, first = 1st *) fun nth 1 (h::t) = h | nth n (h::t) = nth (n-1) t; (* replaces nth element of list with x *) fun replace x n nil = nil | replace x 1 (h::t) = (x::t) | replace x n (h::t) = h::(replace x (n-1) t); fun list x = [x]; (* flattens a list of lists *) fun flat l = fold (fn x => (fn y => x@y)) nil l; (* Painless I/O - what professors would do for students! *) (* call getstring with "getstring()" *) fun getstring () = TextIO.inputLine(TextIO.stdIn); fun getint () = let val (SOME x) = Int.fromString (TextIO.inputLine(TextIO.stdIn)) in x end; fun printint n = print (Int.toString(n)); (* BASIC STRING OPERATIONS: "aa"^"bb" -> "aabb" Int.toString(4) -> "4" *)