2008 spring final solutions - Final Examination Computer...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Final Examination Computer Science 312, Cornell University 13 May 2008 Solutions The smoothed histogram of scores looked like this: 40 50 60 70 80 90 100 0 0.5 1 1.5 2 CS 312 Final Examination, Spring 2008 median The median was 82.5 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
1. True/False [10 pts] (parts a–e) (a) A computation in a thunk is evaluated zero or one times. True (b) In the message-passing approach to concurrency, threads exchange information by storing messages into shared memory references. False (c) To avoid race conditions when writing a thread-safe ADT, mutators must in general acquire locks, but observers that have no side effects need not. False (d) Reference counting can fail to collect some unreachable objects. True (e) When the lambda calculus term MYAPP = λf.λx.xf is applied to two arguments f and x , i.e., ( MYAPP f x ) , the result is the same as the result of applying f to x . False 2. Specification and verification [19 pts] (parts a–d) The following is the interface and a partial implementation of a data abstraction. Unfortunately the implementer was a little confused about the rep invariant and abstraction function, and has gotten off to a bad start. 2
Background image of page 2
signature BAG = sig (* A bag (or multiset) is a unordered collection of elements in which * a given element can appear any number of times. This sig is an * interface for a mutable bag of integers. * Examples: Ø, {0}, {-1, 10, 10} *) type bag (* create() is a new empty bag {} *) val create: unit -> bag (* empty(b) is whether b is empty *) val empty: bag -> bool (* contains(b, x) is whether x is in b *) val contains: bag * int -> bool (* Effects: add(b, x) adds one appearance of x to b. Examples: add({1}, 0) = {0, 1}, add({2}, 2) = {2,2} *) val add: bag * int -> unit (* remove(b,x) is b with one appearance of element x removed. * Examples: After remove(b, 0) with b = {0,0,1}, b = {0, 1}. * After remove(b, 0) with b = {1}, b = {1}. *) val remove: bag * int -> unit end structure Bag :> BAG = struct type bag = {elem: int, count: int} list ref fun create() = ref [] fun empty(b) = case !b of [] => true | _ => false fun contains(b, x) = List.exists (fn ({elem = x’, count = _}) => x = x’) (!b) fun add(b, x) = . .. fun remove(b, x) = if contains(b, x) then b := map (fn ({elem = x’, count = n}) => if x = x’ then {elem = x’, count = n-1} else {elem = x’, count = n}) (!b) else () end (a) [5 pts] Based on the implementations of the operations other than remove , give the rep invariant and abstraction function for this implementation. Answer: (* AF: * AF(ref l) = AF’(l) * where * AF’([]) = Ø * AF’({elem = x, count = n}::t) = AF’(t) {x, x, . .., x} * (n copies of x; the operator denotes bag union.) * * RI: * 1. No element elem appears twice in different list elements. * 2. The count in each list element is positive.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 01/22/2010.

Page1 / 8

2008 spring final solutions - Final Examination Computer...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online