# CS 307: Study Guide for Exam 1: Answers

### Programming:

Write Scheme programs for each of these:
• Given as input a list of numbers, add up the squares of the numbers.
```(define (sumsq l)
(if (null? l)
0
(+ (expt (car l) 2)
(sumsq (cdr l)) ) ) )
```
• Write a function that counts the number of symbols at the top level of a list l. Example: (countsymbols '(a b 7 (peas) #f carrots)) = 3
```(define (countsymbols l)
(if (null? l)
0
(+ (if (symbol? (car l)) 1 0)
(countsymbols (cdr l)) ) ) )
```
• You are given as input a list of items, some of which are numbers and some of which may not be numbers. Write function(s) to compute the average of the numbers in the list (at top level).
```(define (avgnums l)
(let ((sum 0) (count 0))
(dolist (item l)
(if (number? item)
(begin (set! sum (+ sum item))
(set! count (1+ count))) ) )
(/ sum count) ))
```

The following solution uses a tail-recursive auxiliary function:

```(define (avgnums l) (avgnumsb l 0 0))

(define (avgnumsb l sum count)
(if (null? l)
(/ sum count)
(if (number? (car l))
(avgnumsb (cdr l) (+ sum (car l)) (1+ count))
(avgnumsb (cdr l) sum count))))
```
• Write function(s) that count the number of "interesting" symbols in a list lst, given a list int of interesting symbols. It should be (define (int-count int lst) ...), e.g.:
```(int-count '(big dog) '(a big dog has a big appetite))  =  3
```
```(define (int-count int l) (int-countb int l 0))
(if (pair? l)
(int-countb int (cdr l) (+ answer
(if (member (car l) int)
1
0)))
```
```(define (int-count int l)
(let ((count 0))
(dolist (x l)
(if (member x int)
(set! count (+ count 1))) )
count))
```
• Write a function as follows: The inputs are a list, lst, of positive integers and a positive nonzero integer n. The output should be a list containing those elements that are exactly divisible by n. Example:
```(divideby '(3 4 6 17 21 0) 3)  =  (3 6 21 0)
```
```(define (divideby lst n)
(if (pair? lst)
(if (= (modulo (car lst) n) 0)
(cons (car lst) (divideby (cdr lst) n))
(divideby (cdr lst) n))
'() ) )
```
• Write a function as follows: The input is a list, lst, consisting of a student's last name followed by a series of sublists, (item points). The function should return the sum of the points. Example:
```(addpoints '(smith (exam 33) (lab 40) (extra 10)))  =  83
```
```(define (addpoints lst) (addpointsb (cdr lst)))
(if (pair? lst)
0))
```
```(define (addpoints lst)
(let ((total 0))
(dolist (sublist (cdr lst))
(set! total (+ total (cadr sublist))) )
total))
```
• Write a function as follows: The input is a list of sublists; each sublist has the form (department sales costs). Profit for a department is sales minus costs. Find the total profit across all departments in the list. Example:
```(profit '((garden 15000 10000) (kitchen 2000 1000)))
=  6000
```
It is helpful to define small functions that document what is being done:
```(define sales cadr)
(define (deptprofit dept) (- (sales dept) (costs dept)))

(define (profit lst)
(if (pair? lst)
(+ (deptprofit (car lst))
(profit (cdr lst)))
0))
```
Without the documenting functions, the result is correct but less clear:
```(define (profit lst)
(if (pair? lst)
(profit (cdr lst)))
0))
```
• Write a function as follows: The input is an association list of sublists containing a name and a phone number. The output should be an association list containing sublists containing a phone number followed by a name. Example:
```(invert '((john 3271234) (mary 4449876)) )
=  ((3271234 john) (4449876 mary))
```

```(define (invert lst)
(if (pair? lst)
(cons (reverse (car lst))
(invert (cdr lst)))
'()))
```
• Write a function (points lst pts) as follows: lst is a list of symbols, and pts gives the number of points for each kind of symbol. Compute the total number of points for all the symbols in lst. Example:
``` (points '(pawn rook pawn)
'((queen 10) (rook 5) (pawn 1)))  =>  7
```

```(define (points lst pts)
(if (pair? lst)
(+ (cadr (assoc (car lst) pts))
(points (cdr lst) pts))
0))
```
• The sine function can be computed from its Taylor series expansion:
```          1    3    5    7    9
x    x    x    x    x
sin(x) = -- - -- + -- - -- + -- - ...
1!   3!   5!   7!   9!
```
Write a function (sine x) to compute sine using this series. You may not use the functions expt or factorial; instead, write a tail-recursive auxiliary function to compute each term of the series from the previous term and add it to the sum. Stop after the 21st power of x. Hint: write down how each term of the series differs from the previous term.

```(define (sine x) (sineb x 3 (* x x) x))

(define (sineb term n xsq sum)
(if (> n 21)
sum
(let ((newterm (- (/ (* term xsq) (* (1- n) n)))))
(sineb newterm (+ n 2) xsq (+ sum newterm)) ) ) )
```
• Write a function (select major year lst) as follows: lst is a list of sublists of the form (name major birth-year) and year is the current year. The function should return a list of sublists of the form (name age) giving the name and age of each person with the specified major. Example:
```(select 'cs 2000
'((john cs 1980) (mary math 1982) (jane cs 1982)))

=>  ((john 20) (jane 18))
```
```(define (select major year lst)
(if (pair? lst)
(if (eq? (cadr (car lst)) major)
(cons (list (car (car lst))