Review Sheet for the Midterm Exam

Fall 2014


This review sheet is intended to help you study for the midterm exam. It is not intended to be an exhaustive list of everything we've covered, but it should help remind you of the big ideas... .

It's a good idea to review your lecture notes and the solutions to your homework problems.  The lectures' "quizzes" are especially useful in helping you study for the exam, since they're the style and spirit (roughly) of the questions...

Please remember that you may use for the exam a 8.5 by 11 sheet of self-made notes; two-sided notes are OK.

Study Topics

Logical programming with Prolog

Functional programming with Racket...

Run-time analysis

Some Practice Problems

These don't hope to span all of the topics we've covered, but they are meant to give a sense of the size and scope of possible exam problems.

  1. Writing foldr in Racket! Recall the the foldr function in Racket takes as input three arguments: A function f of two variables, a unit (such that (f X unit) => X), and a list L. The foldr function returns the result of repeatedly applying the function f to the elements in L until to reduce them to a single value. For example,
      Racket> (foldr (lambda (X, Y) (+ X Y)) 0 '(1 2 3 4))
    10
    You should assume that foldr "associates" (orders operations) like this: 1 + 2 + 3 + 4 = 1 + (2 + (3 + (4 + 0))). (Notice that the 0 at the end is the unit for addition.) Write the foldr function from scratch. That is, your implementation my use Racket's list notation and recursion, but no built-in functions. Notice that foldr should not be written exclusively to handle addition! It's first argument can be any function of two variables, its second argument is the unit for that function, and its third argument is a list of elements of the type operated on by the given function.

  2. Use-it-or-lose-it in Racket

    Write a function (closest-sum target L) that takes in a numeric target and a list of numbers L. Then, closest-sum should return the subset of the elements of L whose sum is as close as possible to target.

    For example,

    (closest-sum 19 '(3 4 7 7 22))
    '(4 7 7) 
    
    Ties may be handled arbitrarily... any best answer is OK.


  3. Prolog problems

    Part A   Consider this set of lists:

      { L | L consists of N 0's followed by N 1's, for some N >= 0 }
    
      in other words, the possible values of L are
    
      { [], [0,1], [0,0,1,1], ... }
    

    Write a prolog predicate zerones(L,N) that yields true when L is bound to one of these lists and N is bound to the number of zeros (or ones) in the list L. It should yield false if L is not one of those lists or if N is not the correct numeric value. In addition, zerones should be able to generate bindings to L and/or N if those inputs are provided as variables.

    Here are some examples of zerones in action:

    ?- zerones([],0).
    true
    
    ?- zerones([0,0,0,0,0,1,1,1,1,1],N).
    N = 5
    
    ?- zerones([0,0,0,1,1,1,0],N).
    false
    
    ?- zerones([0,1],2).
    false
    
    ?- zerones(L,N).
    L = [], N = 0 ;
    L = [0,1], N = 1 ; ...
    
    ?- zerones(L,3).
    L = [0,0,0,1,1,1]
    

    Part B    Write a prolog predicate

     subseq( L, M )
    
    that is true when the list L is a subsequence of the list M. The idea is that L is a subsequence of M when M contains all of the elements of L in the same order they appear in L, but perhaps M contains some intervening elements, too. You may assume that L and M are both lists. Following are a couple of examples of subseq to clarify its desired behavior:
    ?- subseq( [1, 2, 3], [0, 1, 5, 3, 2, 4] ).
    no.
    
    ?- subseq( [1, 2, 3], [0, 1, 5, 3, 2, 4, 3] ).
    yes.