WRIGHT STATE UNIVERSITY

Department of Computer Science and Engineering

Department of Computer Science and Engineering

CS 784 Spring 2012 Prasad

EOPL 2nd ed. Text. Pages 26-27. Exercise 1.16: 1, 2, 4. Exercise 1.17: 1, 3.

EOPL 3rd ed. Text. Pages 27-29. Exercises 1.18, 1.26, 1.27. Exercises 1.30, 1.34.

We explore the relationship between algebraic (axiomatic)
specification of a (generic) data type and its implementation. You are required to specify the ADT
**Map**^{1}
that supports the following operations described only informally here:

*map*: Yields the empty map.*clear*: Takes a map*m*and yields the empty map.*containsKey*: Takes a map*m*and a key*k*as input, and checks if*m*has*k*.*containsValue*: Takes a map*m*and a value*v*as input, and checks if*m*has*v*bound to some key.*equals*: Takes two maps*m*and*n*as input, and checks if*m*and*n*are equivalent, that is, contain similar bindings.*get*: Takes a map*m*and a key*k*as input, and yields the value bound to*k*in*m*.*isEmpty*: Takes a map*m*as input, and checks if*m*is empty.*put*: Takes a map*m*, a key*k*, and a value*v*as input, and yields a new map that behaves like*m*except that*k*is bound to*v*.*remove*: Takes a map*m*and a key*k*as input, and yields a new map which has*k*deleted from*m*(if it were present)*.**size*: Takes a map*m*as input, and yields the number of key-value pairs in*m*.

- Specify the syntax and semantics of the ADT
**Map**using algebraic (axiomatic) approach. - Give an implementation of the ADT
**Map**in Scheme.

*
Note:* The ADT specs are written
in functional style and as such do not support assignments.
For example,

**What to hand-in?**

Submit your well-documented solutions
to (i) *problem 1 *in __defn.scm__
(containing
Scheme functions
including signature, test calls and outcomes
(see
Design Recipe and the sample given below))
and (ii) *problem 2 *in
__adt.scm__
(containing algebraic specs of the ADT as comments and a
conforming implementation in Scheme) by executing the following turn-in command on
unixapps1.wright.edu. (Make sure
the solution files can be loaded and run in Racket without any modification.)

`%/common/public/tkprasad/cs784/turnin-pa1 defn.scm
adt.scm `

**Sample "Well-documented" Definition**

**; HtDP book Exercise 5.1.2
;** Develop the function

```
check-guess
```

. It consumes two numbers, ```
guess
```

and ```
target
```

. ; Depending on how

`guess`

relates to `target`

, the
function produces one of the following three answers: ```
```

;'TooSmall

, ```
'Perfect
```

, or
`'TooLarge`

.

; **CONTRACT**: check-guess : number number -> { TooSmall, Perfect, TooLarge }

; **PURPOSE**: check-guess takes two numbers - a guess and a target, and

; returns a symbol
TooSmall, Perfect or TooLarge

; depending on
whether target is more, equal or less than guess

; **CODE**:

(define (check-guess guess target)

(cond
[(> guess target) 'TooLarge ]

[(< guess target) 'TooSmall ]

[(= guess target) 'Perfect ]

)

)

; **TEST CASES and EXPECTED OUTCOMES**

;(in general, focus on boundary conditions and coverage)

(check-guess 3 5) "should be" 'TooSmall

(check-guess 5 5) "should be" 'Perfect

(check-guess 7 5) "should be" 'TooLarge

- ...
**Map**^{1} - Reference: java.util.Map