(*world*) (* (1) remove_if p e x Remove from the list x all elements that fail to satisfy the binary predicate p e ex (where ex is a member of x) *) fun remove_if p e [] = [] |remove_if p e (h::t) = if p e h then h::(remove_if p e t) else remove_if p e t; fun bp x y = x=y; remove_if bp 2 [1,2,3,6,1,2,3,1,2,3]; (* (2) equal_if p x y Deliver true if p ex ey holds for pairwise comparisons between members of x and y. Note that x and y are of the same type but may be of different lengths. *) fun possibly_equal x y = (length x) = (length y); fun probably_equal p [] [] = true |probably_equal p (hx::tx) (hy::ty) = (p hx hy) andalso (probably_equal p tx ty); fun equal_if p x y = (possibly_equal x y) andalso (probably_equal p x y); fun equalif p x y = let fun equalx p [] [] = true |equalx p (hx::tx) (hy::ty) = (p hx hy) andalso (equalx p tx ty) in ((length x) = (length y)) andalso equalx p x y end; (* (3) flatten x x is a list of lists. Produce the list that is the result of appending together all lists in x *) fun flatten [] = [] |flatten (h::t) = h@(flatten t); (* (4) min x Deliver the smallest member of the list of integers x What will you do if x=[]? *) exception empty_list; fun min [] = raise empty_list |min (h::t) = let fun minx e [] = e:int |minx e (h::t) = if h (e,i)) x; (* (14) merge p x y Merge the to sorted lists x and y such that the binary predicate p holds over the resultant list. (15) bin_member e x Deliver true if e is a member of the list x. Implement as a "binary chop" using functions first_half and last_half (16) num_to_char n The argument n is an integer. Deliver its string equivalent. That is: num_to_char 129 delivers "129" (17) nums_to_char x Given a list of integers produce a list of string equivalents. Use map. (18) bin_members x Given a list of pairs of the form (e,(ey::y)) deliver true if for every pair e is a member of (ey::y). Create the data set using pair_with over a pair of lists x and y (where y is a list of lists) using map. Implement bin_members using functions bin_member and map (if possible). (19) sort p x Given the binary predicate p and the list x sort the list x with respect to p. Use sort to sort (a) a list of integers in ascending order, (b) a list of integers into ascending order, (c) a list names into alphabetic order, (d) a list of lists such that the resultant list is a sorted list of sorted lists. (20) all_true x Given a list of pairs of the form (p,(e::y)) deliver true if for each pair the predicate p holds over its list. *) fun all_true [] = true |all_true (h::t) = let val (p,x) = h in (all p x) andalso (all_true t) end; (* *)