## Exam 1

### Instructions

• The quiz consists of five questions and one image. Be sure that you have all of these items and that they are all legible.

• Read all questions and their instructions thoroughly before you begin. It is always worth your time to plan ahead!

• You have also been given two blank pages. Use these sheets for your quiz answers. Write your name on each sheet.

• The quiz is worth 60 points. Each question is worth twelve points.

• Keep the quiz questions when you are done, for future reference.

• Points will be awarded based on your explicit answers. Partial credit will be given where possible, so show all of your work.

• The quiz lasts thirty (30) minutes. It is due at 1:45 PM.

### Exercises

1. Write brief answers for each of the following items (two sentences each, max).

• What are the three things that every programming language has?

• Give an example from Racket of each of the three things.

• Explain the distinction in Racket between a function and special form. Illustrate your answer with a specific example of each.

• In Racket, how does the definition of a variable arity function differ from the definition of an ordinary function?

2. Answer each of these questions about the structure of Racket lists.

• Write the Racket literal corresponding to the list shown in this box-and-pointer diagram.

• Draw a box-and-pointer diagram that shows the structure of this Racket list.
```
(cons (list 'a 3) (cons 'b (cons 2 (list 'c 1))))
```

3. Write a Racket function named take-home-pay that takes three arguments, all numbers: an employee's hourly wage, the income tax rate, and the number of hours the employee worked this pay period. take-home-pay returns the employee's total pay after taxes have been deducted. For example:
```     > (take-home-pay 10.00 0.15  6)     ; 6 hrs * \$10.00/hr, minus 15%
51.00

> (take-home-pay 15.00 0.28 40)     ; a decent internship
432.00

> (take-home-pay  4.25 0.00 55)     ; minimum wage, in the old days
233.75
```

4. Write a Racket function named sum-of-apps that takes one arguments: a function, f, of one argument. sum-of-apps returns a two-argument function (both numbers), which applies f to both arguments and returns the sum of those values. For example:
```    > (define double (lambda (n) (* n 2)))
> (define square (lambda (n) (* n n)))

> (define sum-of-squares (sum-of-apps square))

> ((sum-of-apps double) 3 4)              ; (3*2) + (4*2)
14                                        ; == 14
> ((sum-of-apps square) 3 4)              ; 3² + 4²
25                                        ; == 25
> (sum-of-squares 5 12)
169
```

5. Suppose that we have a list containing height/weight pairs for a group of people, in inches and pounds, respectively:
```     ( (76 . 195) (81 . 212) (79 . 225) (71 . 206) ... )
```
We would like to know the average height of the people in the group who weigh more than some given amount.

Use the higher-order functions map, apply, and filter to write a Racket function named avg-height-over that takes two arguments: a weight w and a list of height/weight pairs lst in the above form. avg-height-over finds the subset of lst who weigh more than w and returns the average height of the people in the subset. For example:
```     > (avg-height-over 200 '((79 . 225))
79

> (avg-height-over  90 '((70 . 150) (62 . 100)))
66

> (avg-height-over 200 '((76 . 195) (81 . 212) (79 . 225) (71 . 206)))
77
```
Assume that we have already written a variable-arity function named average. You may write other helper functions if you like, but you do not have to. No recursion or looping are allowed!

Eugene Wallingford ..... wallingf@cs.uni.edu ..... January 31, 2018