Homework Assignment 5

More Structurally Recursive Functions


CS 3540
Programming Languages and Paradigms
Spring Semester 2018


Due: Tuesday, February 20 at 8:00 AM


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
Download the files and use them as templates for your submission. Please use the names given for your files.

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...
Organizing Code



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)
                     (* result (add1 n)))
                   1
                   '(1 2 5))
         36
    


Deliverables

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

Be sure that your submission follows the submission requirements.



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