# CS 307: Study Guide for Exam 2: Answers

### Programming:

Write Scheme programs for each of these:
• Given as input a list structure (tree) containing some numbers (and possibly other things), write a function to add up the cubes of all the numbers.
```(define (cubes x)
(if (pair? x)
(+ (cubes (car x))
(cubes (cdr x)))
(if (number? x)
(expt x 3)
0) ) )
```

• Write a function (count lst tree) that will count the number of occurrences of items in the list lst in the list structure tree. The items in list lst will be simple items such as symbols, numbers, or boolean values. Example:
```   (count '(a 3) '((b 3 a) (c 3 (7 a))))  =>  4
```
```(define (count lst tree)
(if (pair? tree)
(+ (count lst (car tree))
(count lst (cdr tree)))
(if (member tree lst)               ; or memv
1
0) ) )
```

• Write a function that will return the sum of the even numbers in a list structure (tree) that may contain things that are not numbers.
```(define (evensum x)
(if (pair? x)
(+ (evensum (car x))
(evensum (cdr x)))
(if (and (number? x) (even? x))
x
0) ) )
```

• Write a function (total items prices) that will compute the total cost of a list of items. prices is a list ((item price) ...). Example:
```   (total '(bread milk) '((eggs .69) (milk 1.89) (bread 1.39)))
=> 3.28
```
```(define (total items prices)
(let ((sum 0))
(dolist (item items)
(set! sum (+ sum (cadr (assoc item prices)))) )
sum))
```

• Write a recursive function that translates a prefix expression (as in Lisp) to an infix expression (as in Pascal). Assume operators + - * / , where - can be either unary or binary. Example:
```   (infix '(+ a (* b c)))  =>  (a + (b * c))
```
```(define (infix x)
(if (pair? x)
(if (pair? (cddr x))            ; test if binary
(list (infix (cadr x))              ; binary
(car x)
(list (car x) (infix (cadr x)) ) )  ; unary
x))
```

• Write a function (average lst wts) to compute a weighted average of grades. lst is a list of sublists (item grade). wts is a list of sublists (item weight) in arbitrary order. For each element of lst, look up the corresponding weight for that item, multiply the grade by it, and add to the total. Example:
```   (average '((midterm 80) (homework 75) (final 90))
'((homework .2) (final .5) (midterm .3)))
=  .3 * 80 + .2 * 75 + .5 * 90  =  84
```

```(define (average lst wts)
(let ((sum 0))
sum))
```

• Write a function (weight tree lst) to compute the "weight" of a tree structure. Each pair has a weight of 2. Symbols that are in lst have a weight of 3. All other items have a weight of 0. Example:
```   (weight '((a 3) (b c d)) '(a b))  =  20
```

```(define (weight tree lst)
(if (pair? tree)
(+ 2 (weight (car tree) lst)
(weight (cdr tree) lst))
(if (and (symbol? tree)
(member tree lst))
3
0)))
```

• Write a function (path goal tree) that will return the list of successive car's and cdr's necessary to reach the first occurrence of the simple item goal in the structure tree. If goal does not occur in tree, return #f. Example:
```   (path 'c '((a b) (c d)))  =  (cdr car car)
```

```(define (path goal tree)
(if (pair? tree)
(if (path goal (car tree))
(cons 'car (path goal (car tree)))
(if (path goal (cdr tree))
(cons 'cdr (path goal (cdr tree)))
#f))
(if (eqv? goal tree) '() #f)))
```

• Write a function (opposite lst opp) that returns a list of the opposite of each item in lst. opp is a list of sublists, ((item opposite) ...). If an item in lst does not have an opposite, nothing is included in the output for it. Example:
```(opposite '(up cat ernie) '((ernie bert) (up down)))
=  (down bert)
```

```(define (opposite lst opp)
(if (pair? lst)
(if (assoc (car lst) opp)
(cons (cadr (assoc (car lst) opp))
(opposite (cdr lst) opp))
(opposite (cdr lst) opp))
'()))
```

• A student likes Scheme so much that he writes his grocery list as an expression using the operators + and *. Write a function (cost groceries prices) to calculate the cost of the groceries given a list of dotted pairs ((item . price) ...). Example:
```(cost '(+ bread (* 2 milk) (+ nuts (* 2 (* 6 beer))))
'((beer . 0.8) (milk . 1.2)(nuts . 2)(bread . 1.5)))
=  15.50
```

```(define (cost groceries prices)
(eval (sublis prices groceries)))
```
Is there a harder way to do it? Sure, lots of 'em.

• Write a function (largest tree) that finds the largest number in a tree. The tree contains at least one number. You may assume that all the numbers are positive. Example:
```(largest '((+ a 7) b (11 c)))  =  11
```

```(define (largest tree)
(if (pair? tree)
(max (largest (car tree))
(largest (cdr tree)))
(if (number? tree) tree 0)))
```

• A mobile is a symbol, a number, or a list of two mobiles. A mobile is balanced if it is a symbol or a number, or if it is a list, its sub-mobiles are balanced, and its sub-mobiles weigh the same. Symbols have a weight of 1; numbers weigh the amount of the number, and other things weigh nothing. Write a function (balanced m) to determine whether a mobile m is balanced; write other functions if needed. Example:
```(balanced '((1 A) 2) )  =  #t
```

```(define (balanced m)
(if (pair? m)
(and (balanced (car m))
(= (weight (car m)) (weight (cadr m))))
#t))

(define (weight m)
(if (pair? m)
(+ (weight (car m)) (weight (cadr m)))
(if (symbol? m)
1
(if (number? m) m 0) ) ) )
```
Note that these functions recurse on car and cadr (not cdr) since those are the two sub-mobiles.

• Write a function (total items bargains prices) as follows. items is a list ((quantity item) ...) giving items purchased. bargains is a list (item ...) of items that are on sale for 1.00 . prices is a list ((item price) ...) of item prices. Any item that is not in bargains or prices is free. Compute the total cost of all items purchased. Example:
```(total '((6 beer) (1 milk) (1 bag)) '(beer nuts bread)
'((oats 2.00) (milk 1.69) (apple .50)))
=>  7.69
```
```(define (total items bargains prices)
(totalb items bargains prices 0))

(define (totalb items bargains prices sum)
(if (pair? items)
(totalb (cdr items) bargains prices
(+ sum
(* (caar items)
1
0)))))
sum))
```

• Write a function (weight tree) to compute the weight of a tree structure. Each pair has a weight of 2. symbols have a weight of 1. numbers have a weight equal to the number value. Anything else weighs 0. Example:
```  (weight '((a 3) 7))  =>  19  ; 4 pairs, 1 sym, 3, 7
```
```(define (weight tree)
(if (pair? tree)
(+ 2 (weight (car tree)) (weight (cdr tree)))
(if (symbol? tree)
1
(if (number? tree)
tree
0))))
```

• A family tree is a list (mother name father) where mother and father are family trees (or #f if unknown) and name is a symbol. Write a function (relative tree name) that returns a list giving the relation of name to the person whose family tree is given by the argument tree. Examples:
```  (relative '(#f john #f) 'john)  =>  ()    ; john himself
(relative '((#f mary (#f bill #f)) john (#f fred #f))
'bill)
=>  (mother father)  ; bill is john's mother's father
```
```(define (relative tree name)
(if (pair? tree)
'()
(if (relative (car tree) name)
(cons 'mother (relative (car tree) name))
(cons 'father (relative (caddr tree) name))
#f)))
#f))
```

• A car approaching an intersection can go one of three ways: left, straight, or right. An intersection is represented as a list (left straight right) of possibilities. Each possibility is an intersection or a destination (non- pair). Write a function (follow int directions), where int is an intersection, that will follow the list of directions (each of which is L, S, or R) and return the subtree or destination denoted by the directions. If the directions continue past the intersection lists, return #f. Examples:
```  (follow '(a b c) '(r))  =>  c  ; right turn goes to c
(follow '(a (b c (d e f)) g) '(s r l))  =>  d
; straight to (b c (d e f)), right to (d e f), left to d
```
```(define (follow int directions)
(if (pair? directions)
(if (pair? int)
(case (car directions)
((l) (follow (car int) (cdr directions)))