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.
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.
Racket> (foldr (lambda (X, Y) (+ X Y)) 0 '(1 2 3 4))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.
10
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,
Ties may be handled arbitrarily... any best answer is OK.
(closest-sum 19 '(3 4 7 7 22))
'(4 7 7)
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], ... }
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.