## Homework Assignment 5

### More Structurally Recursive Functions

#### Introduction

This assignment asks you to write a few recursive functions in Racket. The primary goal of this assignment is to gain more experience with common recursion patterns. For this assignment:

Template Source File

These file include a provide clause and a require clause, respectively. The former exports your five public functions for use by other files, and the latter imports your functions, enabling you to test them with Rackunit.
Do Not Use...
• ... any of Racket's primitive higher-order functions in any of your solutions, including map, apply, and filter.
• ... any function that converts a list argument to another datatype. Process the list.
• ... a let expression or an internal define in any of your solutions.
Organizing Code
• Put your solutions in homework05.rkt. Use a comment to indicate where the code for each problem begins and ends. Put any helper functions you write for a problem after the main function of your solution.
• Put your tests in homework05-tests.rkt. For each problem, write at least three check-equal? expressions to test your solution. You may use my examples as one of your tests. Be sure that you test other key cases, too.

#### Problems

1. Write a structurally recursive function (list-of? type? lst) that takes two arguments: a boolean predicate type? and list lst. list-of? returns a true if every item in lst is of the type?. For example:
```     > (list-of? number? '(1 2 3 "Eugene" 4 5))
#f
```
2. Write a mutually recursive function (nlist-abs nlist) that takes one argument, an n-list named nlist.
```               <n-list> ::= ()
| (<number-exp> . <n-list>)
<number-exp> ::= <number>
| <n-list>
```
nlist-abs returns an n-list identical in all respects to nlist except that each value have been replaced by its absolute value. For example:
```     > (nlist-abs '((((((1 -2 (3 -4)) 5 -6 ((7 (-8) 9 -10))) 11 -12)) 13 -14)))
'((((((1 2 (3 4)) 5 6 ((7 (8) 9 10))) 11 12)) 13 14))

> (nlist-abs '(1 1 (-1) (-7 (21 0 21) 3) -4 (5 1)))
'(1 1 (1) (7 (21 0 21) 3) 4 (5 1))
```
nlist-abs should be mutually recursive with the function numberexp-abs, which returns the absolute value of a number expression. Recall that Racket includes an abs function.

3. Write a mutually recursive function (max-length str-list) that takes one argument, a string-list.
```          <string-list> ::= ()
| (<string-exp> . <string-list>)
<string-exp> ::= <string>
| <string-list>
```
max-length returns the length of the longest string in str-list. If str-list is the empty list, it returns -1. For example:
```     > (max-length '("Write" "a" "mutually" "recursive" "function"
("max-length" "str-list")
"that" "takes" "one" "argument"
("a" "string-list")))
11
```
max-length should be mutually recursive with the function max-length-se, which processes a a string expression.

Recall that Racket provides the max and string-length functions.

4. Write a mutually recursive procedure (prefix->infix binary-exp), which takes a binary expression in prefix notation as an argument.
```           <prefix-exp> ::= (<operator> <number-exp> <number-exp>)
<number-exp> ::= <number>
| <prefix-exp>
<operator> ::= <symbol>
```
prefix->infix returns an equivalent infix expression as its value.
```            <infix-exp> ::= (<number-exp> <operator> <number-exp>)
<number-exp> ::= <number>
| <infix-exp>
<operator> ::= <symbol>
```
For example:
```     > (prefix->infix '(+ 4 5))
'(4 + 5)

> (prefix->infix '(* (+ 4 5)
(+ 7 6)))
'((4 + 5) * (7 + 6))
```
prefix->infix should be mutually recursive with the function number-expr>infix, which returns an infix version of a number expression. (This is a great place to use program derivation, if you like.)

5. Write a structurally recursive function (reduce f init lon) that takes three arguments, a two-argument function f, a number init, and a list of numbers lon. reduce walks down the lon, applying f to each item and to the value of init, using the result as the next value for init. At the end, reduce returns the value of init. For example:
```     > (reduce + 0 '(1 2 3 4))      ; ((((0 + 1) + 2) + 3) + 4)
10

> (reduce - 100 '(1 2 3 4))    ; ((((100 - 1) - 2) - 3) - 4)
90

> (reduce (lambda (result n)
1
'(1 2 5))
36
```

#### Deliverables

By the due time and date, submit the following files:

• homework05.rkt, the source file containing your function definitions
• homework05-tests.rkt, the source file containing your test cases

Be sure that your submission follows the submission requirements.

Eugene Wallingford ..... wallingf@cs.uni.edu ..... February 15, 2018