CS-115 Midterm
CS-115 Midterm
CS-115 Midterm
CS115 Waterloo
Midterm Examination
Term: Spring Year: 2012
Disclaimer:
CS115 material may change from semester to semester, and
different instructors may stress different concepts. So, your
midterm may cover material not covered on this exam, or may
cover similar material in a different way.
If your instructor indicates that a Reference Sheet will be provided,
it will likely be similar to the one attached to this exam, but may
not be identical. Additional functions may be added, and others
removed.
Signature: ____________________
Total 50
(define x 10)
(+ (x 10) 22)
(define w 10)
(define (g y) (+ w (- y 1)))
(g 1)
(and (< 50 100) (zero? (- 2 1))
(substring "a" 3 10))
(substring "abcdefgh" 4 6)
(cond
[(odd? 10) 'One]
[(odd? 20) 'Two]
[(odd? 30) 'Three])
(cond
[(even? 10) 'One]
[(even? 20) 'Two]
[(even? 30) 'Three])
(define L (cons 6 (cons 7 empty)))
(rest (first (rest L)))
Use these definitions to simplify the following Scheme expression. Be sure to write a
complete trace of all steps in the simplification process. Use one line per step. Not all the
lines may be needed.
(define x 10)
Use these definitions to simplify the following Scheme expression. Be sure to write a
complete trace of all steps in the simplification process. Use one line per step. Not all the
lines may be needed.
(* (cond
[(odd? x) (* 2 3)]
[else 0])
x)
+ ,
, =
0,
+ ,
,
2 , < 0
where
= 100, = 0
4 , > 0.
[1 mark] Compete the constant definition create a constant my-dog, and set its value to be a
brown dog named Max, who is 3 years old.
(define my-dog
[1 mark] Based on the information provided in the data definition above, write the contract
for the selector function dog-colour.
;; dog-colour:
[1 mark] Complete the body of the function same-name? that consumes two dog
structures, dog1 and dog2, and produces true if they have the same name, and false
otherwise. The function has been started for you.
[1 mark] Write an example for combine-posns using posn values corresponding to (2,3)
and (-2,-1) and the symbol 'add.
(define (modify L)
(cond
[(empty? L) (cons false empty)]
[(negative? (first L))
(cons (* 2.0 (first L)) (modify (rest L)))]
[(positive? (first L))
(cons (* 3 (first L)) (modify (rest L)))]
[else (cons 'zero empty)]))
[2 marks] Write a contract for modify (assume that there will be no errors when modify is
called).
;; modify:
[3 marks] What are the final values for the following expressions?
(modify empty)
This page as been left blank intentionally. Use this space if necessary to complete your
answers. Make sure you note on the original page that more of your solution can be
found here, and be sure to label your work with the appropriate question number.
For this exam, the Scheme language level is Beginner Student Scheme. You may find
these Scheme functions helpful.
(cons v lst) constructs a list with first element v followed by list lst.
(empty? lst) produces true if lst is empty, and false otherwise.
(first lst) produces the first element in the nonempty list lst.
(length lst) produces the number of elements of the list lst.
(member v lst) produces true if v is in the list lst, and false otherwise.
(rest lst) produces the rest of the nonempty list lst.
(string-append s t) produces a string containing all the characters in s
followed by all the characters in t.
(string-length s) produces the number of characters in the string s.
(substring s a b) produces a string consisting of the characters in s from
positions a to b-1, inclusive.
(substring s a) produces a string consisting of the characters in s from
positions a to the end of the s.
(make-posn x y) creates a posn object.
(posn-x p) produces the x-value of posn p.
(posn-y p) produces the y-value of posn p.
(equal? x y) produces true if x and y are equal, false otherwise.
(expt x y) produces x raised to the power of y.
(even? n) produces true if n is even and false if n is odd. Note 0 is even.
(integer? n) produces true if n is an integer, false otherwise.
(max x y) produces the larger of x and y.
(min x y) produces the smaller of x and y.
(negative? n) produces true if a number n is less than 0, and false if it is
greater or equal to 0.
(odd? n) produces true if n is odd and false if n is even.
(positive? n) produces true if a number n is greater than 0, false if it is less
than or equal to 0.
(quotient m d) produces the quotient (integer part) when m is divided by d.
(remainder m d) produces the remainder when m is divided by d.
(check-expect act exp) tests whether act and exp are equal.