Computer Science 3675
Fall 2014
Practice Questions for Quiz 3

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

    1. map (x |-> x + 5) [2,4,6]
    2. map tail [[2,5], [4], [6,5,4]]
    3. foldLtoR (1,(+)) [2,3,4,5]

    Answer

  2. Function select is written so that (select p x) returns the first member m of list x such that p(m) is true. (For the purposes of this question, it will not matter what select does when there is no such m.) Notice that p is a predicate, a function that produces a boolean result. Predicate odd? returns true on an odd number and false on an even number. For example, select odd? [2,3,4,5,6] = 3. (Notice that the question mark is part of the name of function odd?.)

    1. What is the type of function odd?

    2. What is the polymorphic type of function select? Keep in mind that select is curried. Also, keep in mind that the list does not need to be a list of integers.

    3. 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. Given definition

       f x y = (x+1)::y.
    

    1. What kind of thing is x (a number, a list or a function)?
    2. What kind of thing is y (a number, a list or a function)?
    3. What kind of thing is f x y (a number, a list or a function)?
    4. What kind of thing is f x (a number, a list or a function)?

    Answer

  4. 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)?
    2. What kind of thing is parameter y? (A number, a list, a function, or something else)?
    3. What kind of thing does expression f(2) yield? (A number, a list, a function, or something else? Is this expression allowed?)

    Answer

  5. 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 definition of 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].

      Answer

    2. Write a definition of 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.

      Answer

  6. Consider the following definition written in Cinnameg.

        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.) Evaluating a promise does not change the value that is represented, it only changes the representation of the value.

    Answer

  7. Given the Cinnameg type definition

      Type Frog =
          green String
        | yellow (Integer, Integer)
    
    Which of the following are sensible?
    1. Let kermit = frog("Kermit", 3, 4).
    2. Let kermit = green("Kermit").
    3. Let kermit = yellow("Kermit").
    4. Let kermit = yellow(3,4).
    5. Let yellow(x,y) = yellow(4,7).
    6. Let green(s) = yellow(4,7).

    Answer

  8. Using the type Frog from the preceding question, define a function size(f) so that: if f is a Frog of the form green(name), then size(f) is the length of name; and if f is a Frog of the form yellow(m,n) then size(f) is m.

    Answer