Computer Science 3675
Fall 2017
Practice Questions for Quiz 3

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

    Answer

  2. What is the value of Scheme expression (cons 'salamander (list 'frog 'toad))

    Answer

  3. Write a Scheme function called prefix so that (prefix x y) returns #t if list x is a prefix of list y (and #f if not). It is not necessary for x to be a proper prefix of y. Every list is a prefix of itself, and the empty list is a prefix of every list. Be careful to use Scheme syntax correctly.

    Answer

  4. 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. Be careful to use Scheme syntax correctly.

    Answer

  5. Write a Scheme function called drop so that (drop n L) returns the result of removing the first n members from list L. If L has fewer than n members, then (drop n L) should return the empty list. For example, (drop 2 '(4 8 3 5 7)) = (3 5 7) and (drop 3 '(5 2)) = ( ). Be careful to use Scheme syntax correctly.

    Answer

  6. Suppose that function f is defined by

    f x y = y x
    What is the most general polymorphic type of f? Use Greek letters for type variables. Notice that f is curried.

    Answer

  7. Suppose that function f is defined by

    f x y z = x [y::z]
    What is the most general polymorphic type of f? Use Greek letters for type variables. Notice that f is curried.

    Answer

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

  9. Consider the following definition written in Cinnameg.

        Define f(n) = (: n :: f(n*n) :).
    
    What value does expression f(2) compute? Give the full value, as it would be if it were examined.

    Answer

  10. What is the value of each of the following Cinnameg expressions? (foldLtoR i f x) is a left-to-right fold with initial value i and function f on list x.

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

    2. map tail [[2,5], [4], [6,5,4]]. Answer

    3. foldLtoR 1 (+) [2,3,4,5]. Answer

  11. 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 integer and false on an even integer. (The question mark is part of the name of function odd?.) For example, (select odd? [2,3,4,5,6]) = 3.

    1. What is the type of function odd? Answer

    2. What is the most general polymorphic type of function select? Keep in mind that select is curried. Answer

    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

  12. By choosing among the operations foldLtoR, foldRtoL and map write a Cinnameg definition for each of the following that does not make direct use of 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 product so that product(x) yields the product of all of the members of list x, where x is a list of numbers. (Product means multiply.)

      Answer

  13. Given definition

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

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