Computer Science 3675
Fall 2003
Practice questions for quiz 2

  1. Given the definition

          f([])   = []
          f(h::t) = (h*h)::f(t)  when h > 10
          f(h::t) = f(t)         when h <= 10
      
    show an inside-out evaluation of expression f([4,12,15,6,11]). Assume that arithmetic is done as soon as possible.

  2. Write an equational definition of a function called smallest so that smallest(n,x) is the smallest member of list n::x. For example, smallest(3, [6,4,7]) = 3 and smallest(8, [2,5]) = 2. You may presume that you have a function called min that takes the minimum of two numbers. For example, min(7,4) = 4.

  3. Show an inside-out evaluation of expression smallest(5, [9,4,6,2]), using your definition from the preceding problem.

  4. Write a function called positives that takes a list of numbers and produces a list of the positive numbers in that list, preserving order. For example, positives([4, -6, -1, 7, 3, -2] = [4, 7, 3]. A number x is positive if x > 0.

  5. In a purely functional language, is it ever possible to compute the same expression twice in the same context and get different values? For example, if E is an expression, and you compute E two times, one right after another, could the first computation yield a different result from the second computation? Why or why not? (Note: Astarte is not a purely functional language. Think about the functional subset of Astarte. What is functional programming?)

  6. Answer the preceding question, but instead of for a purely functional language, for an imperative language such as C++.

  7. 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].

    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.

  8. Function positives from a prior question is an example of a function that filters a list, keeping only members that satisfy a predicate. Write two definitions of a general filter function so that filter p L produces a list of all members x of list L such that p(x) is true. For example, positives is just filter(pos) where pos(x) is true when x > 0.

    1. Write filter using a list comprehension.

    2. Write filter without a list comprehension.

  9. Consider the following definition written in Astarte.

        Define f(?n) = (:n*n :: f(n+1):).
      
    What value does expression f(2) compute? Give the full value, as it would be if it were examined. (It suffices to give a clear description of the full value.)

  10. What would happen in the preceding question if f were defined by

        Define f(?n) = n*n :: f(n+1).
      
    instead? Now what does expression f(2) do?