Computer Science 3675
Section 001
Fall 2006
Solutions to practice questions for quiz 3

  1. Write an equational definition of a function suffix so that suffix(x,y) is true just when list x is a suffix of list y. (List x is a suffix of list y if there exists a list z such that z ++ x = y.) For example, suffix([4,6], [1,2,4,6]) is true, but suffix([5,6], [1,5,2,6]) is false. The empty list is a suffix of every list, and every list is a suffix of itself.

    Keep it simple. Here are two approaches.

    1. List x is a suffix of list y if the reversal of x is a prefix of the reversal of y. So a definition of suffix is

      suffix(x,y) = prefix(reverse x, reverse y)
      prefix([], y) = true
      prefix(a::r, b::t) = a == b and prefix(b,t)
      prefix(a::r, []) = false

    2. To determine whether list x is a suffix of list y, get the suffix s of y whose length is the same as the length of x. Then just check whether s = x.

           suffix(x,y) = (ly >= lx) and ((drop (ly - lx) y) == x) |
                           lx = length x
                           ly = length y
          
      We wrote the definition of drop in class.
      drop 0 y = y
      drop n [] = []
      drop (n+1) (h::t) = drop n t

  2. What is the value of each of the following Cinnameg expressions?

    1. map (?x => x + 5) [2,4,6] (Answer: [7,9,11])
    2. map tail [[2,5],[4],[6,5,4]] (Answer: [[5],[],[5,4]])
    3. foldLtoR (1,+)[2,3,4,5] (Answer: 15)
  3. Function select is written so that (select p x) returns a the first member m of list x such that p(m) is true. Notice that p is a predicate, a function that produces a boolean result. (For the purposes of this question, it will not matter what select does when there is no such m.) Predicate odd? returns true on an odd number and false on an even number.

    What is the value of Cinnameg expression map(select odd?)[[2,3,4,5],[1,2,3,4],[2,4,6,7,8]]? (Answer: [3,1,7])

  4. Given definition

        f x y = (x+1)::y.
      
    1. What kind of thing is x (a number, a list or a function)? (Answer: A number)
    2. What kind of thing is y (a number, a list or a function)? (Answer: A list)
    3. What kind of thing is f x y (a number, a list or a function)? (Answer: A list)
    4. What kind of thing is f x (a number, a list or a function)? (Answer: A function)
  5. You are given the following function definition.

         f x y z = y x (x::z)
      
    1. What kind of thing is parameter z? (A number, a list, a function, or something else)? (Answer: A list)
    2. What kind of thing is parameter y? (A number, a list, a function, or something else)? (Answer: A function)
    3. What kind of thing does expression f(2) yield? (A number, a list, a function, or something else? Is this expression allowed?) (Answer: A function)

  6. By choosing among the operations foldLtoR, foldRtoL and map write a definition for each of the following that does not use recursion or loops.

    1. Write a function called doubleAll that takes a list x of numbers as its parameter and produces a list of the doubles numbers in list x as its result. For example, doubleAll([5,2,19,3]) = [10,4,38,6].

      double x = 2*x
      doubleAll = map double

    2. Write a function called maxlist(m,L) that computes the largest value in list m::L. That is, it computes the maximum of m and the largest value in list L. Assume that function max is available where max(x,y) is the larger of x and y.

      maxlist(m,L) = foldLtoR(m,max) L
      (foldRtoL works as well. The direction does not matter.)

  7. What is the value of Scheme expression (car (cdr (cons 'horse (cons 'zebra ()))))?

    Answer: zebra. Be sure to pay attention to the difference between zebra and (zebra). For example, expression (cdr (cons 'horse (cons 'zebra ()))) yields (zebra).

  8. Write a Scheme function called prefix so that (prefix x y) returns true if list x is a prefix of list y. Be careful to use Scheme syntax correctly.

      (define (prefix x y) 
        (cond
           ((null? x) #t)
           ((null? y) #f)
           ((eq (car x) (car y)) (prefix (cdr x) (cdr y)))
           (else #f)
        )
      )
      

  9. Write a Scheme function called descending so that (descending L) is true for a list of numbers L if L is in descending order. (The Scheme function < does a less-than test, and > does a greater-than test.) List (a b c d) is in descending order if a > b > c > d. The empty list is in descending order by definition, as is a singleton list.

      (define (descending L) 
        (cond
           ((null? L) #t)
           ((null? (cdr L)) #t)
           ((> (car L) (cadr L)) (descending (cdr L)))
           (else #f)
        )
      )