Homework 4:
Structurally Recursive Functions

Due: Monday, February 19, at 11:59 PM

Introduction

This assignment asks you to write recursive functions in Racket. The primary goal of this assignment is to gain experience with recursion and Racket lists.

Template Source File

Download this template file and use it as the starting point for your submission. Please name your file homework04.rkt.

This file includes a provide clause that exports your five public functions. This enables users to load your module and run your functions. It also enables me to test your code using my own Rackunit tests.

With provide, you must define all five functions. If you don't have time to solve a problem, define a function that takes the correct number of arguments and returns a legal default value, such as 0 or '().

Do Not Use...

To solve these problems, you do not need any Racket features beyond the things we have learned in class and the things discussed in this assignment. In order to practice the new skills we are learning, do not use...

Organizing Code

Use a comment to indicate where the code for each problem begins and ends. The template already does that for you.

For each problem, write at least three test expressions to test your solution. Depending on the type of value that the function produces, use check-equal? or check-true/check-false. You may use my example as one of your tests. Be sure that you test other key cases, too.

Put any helper functions you write for a problem between the main function in your solution and the tests for the main function. (You are not required to write any helper functions.)

Problems

  1. Write a structurally recursive function named (every? lob) that takes a list of booleans lob as its argument. A list of booleans has the same structures as a list of numbers or a list of symbols, except the first item in each pair is a boolean value. every? returns #t if all of the values in lob are #t, and #f otherwise. For example:
    > (every? '(#t #t #t))
    #t
    
    > (every? (map (lambda (n) (<= n 40))
                   '(26 37 41 25 12)))     ; 41 is bigger than 40
    #f
    

  2. Write a structurally recursive function named (reject pred? lst) that takes two arguments, a one-argument predicate pred? and a list lst of values of the type accepted by pred?. reject returns a list containing all the items from lst that fail pred?. For example:
    > (reject negative? '(1 2 -1 -2 3 -3 5 -4 4))
    '(1 2 3 5 4)
    

  3. Write a structurally recursive function named (interleave lst1 lst2) that takes as arguments two lists, lst1 and lst2. interleave returns a list of pairs, where each pair consists of the items at the corresponding positions in lst1 and lst2. The process stops when either list runs out of items. For example:
    > (interleave '(a b c d e) '(1 2 3 4 5))
    '((a . 1) (b . 2) (c . 3) (d . 4) (e . 5))
    
    > (interleave '(a b c) '(1 2 3 4 5))
    '((a . 1) (b . 2) (c . 3))
    

  4. Write a structurally recursive function named (cons-at-end v lst) that takes two arguments: any Racket value v and a list lst. cons-at-end returns a list containing all of the items in lst followed by v. For example:
    > (cons-at-end 'e '(a b c d))
    '(a b c d e)
    
    Be sure to think about the answer to return in the base case.

  5. Write a structurally recursive function named (positions-of s los) that takes two arguments, a symbol s and a list of symbols los. positions-of returns a list containing the zero-based positions of all occurrences of s in los. For example:
    > (positions-of 'a '(a b a c d a e f g a h i j k))
    '(0 2 5 9)
    
    Make positions-of an interface procedure that calls a structurally recursive helper function with the symbol and list of symbols as the first two arguments, and an initial value for the counter as the third argument.

    Note: We will discuss interface procedures in class on Thursday, Session 10.

Deliverables

By the due time and date, use the course submission system to submit the following files electronically:

Be sure that your submission follows the submission requirements. Be sure to use the specified name for your file. This enables the autograder to find and run your code.