CSE305 Homework #3, Question 1 due at beginning of class 7/12/02, on-line portion to be submitted midnight 7/12/02

  1. Problem 8.2 in text, page 335. (Pencil and paper). (10 pts each)

  2. Write the following functions in ML. You may write helper-functions as needed. The signatures are given, yours must match. Your functions must be named exactly, although the parameter names are up to you. You should not provide test code, we will write our own. (5, 5, 10, 10 = 30 points)
    1. less(k, lst) to return a list consisting of all integers in lst that are less than k.
      fn : int * int list -> int list
    2. adjacent(lst) to determine whether there are two adjacent occurences of an element in list lst.
      fn : ''a list -> bool
    3. cycle(lst, i) cycle lst i times. E.g. cycle([1, 2, 3, 4, 5], 3) => [4, 5, 1, 2, 3].
      fn : 'a list * int -> 'a list
    4. duplicate(lst) given the list [a1, a2, ..., an] produce the list [a1, a1, a2, a2, ..., an, an].
      fn : 'a list -> 'a list
    Filename: basic.sml

  3. Given the following definition of reduce, fill in the blanks on the functions below (note: you are to use anonymous functions) Remember to include your definition of reduce in the file you submit: (5, 5, 5, 5, 10 = 30 pts)
           fun reduce (f,v,nil) = v
            |  reduce (f,v,a::y) = f(a,reduce(f,v,y));
    
           fun length lst = reduce(______,_______,_______);
           fun append (lst1,lst2) = reduce(_______,_____,_______);
               (* append lst1 and lst2 *)
           fun remove_if (f,lst) = reduce(_____,_____,_______);
               (* e.g. remove_if(fn(x) => x = 1, [1,2,1,3,1,4,1,5]) =
                              [2,3,4,5] *)
           fun map (f,lst)  = reduce(_____,_____,_______);
               (* the map function *)
           fun suffixes(lst) = reduce(_____,_____,_______);
               (* e.g. suffixes([1,2,3]) = [[1,2,3], [2,3], [3], []]; *)
           
    Filename: higher.sml