Homework 4:
Structurally Recursive Functions

Due: Monday, February 23, 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...

  • ... any of Racket's primitive higher-order functions, including map, apply, and filter.
  • ... reverse or any Racket function that converts a list argument to another datatype. Process the list one element at a time.
  • ... a let expression or an internal define in any function.

Organizing Code

Use a comment to indicate where the code for each problem begins and ends. The template already does that for you, if you would like to keep its organizing comments in place.

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 one of my examples as one of your tests. Be sure that you test other key cases, too.

If you have any questions about how a function should behave, be sure to ask for a clarification before writing your code!

You are not required to write helper functions for any of the problems. + If you do, though, put the helper functions for a problem between the main function in your solution and the tests for the main function. You do not have to write separate tests for the helper but may if you like.

Data Definitions

The problems refer to these inductive data definitions:

 <list-of-symbols> ::= ()
                     | (<symbol> . <list-of-symbols>)

 <list-of-numbers> ::= ()
                     | (<number> . <list-of-numbers>)

<list-of-booleans> ::= ()
                     | (<boolean> . <list-of-booleans>)

            <list> ::= ()
                     | (<any> . <list>)

Problems

  1. Write a structurally recursive function named (every? lob) that takes a list of booleans lob as its argument. 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 (partner lst1 lst2) that takes as arguments two lists, lst1 and lst2. partner 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:
    > (partner '(a b c d e) '(1 2 3 4 5))
    '((a . 1) (b . 2) (c . 3) (d . 4) (e . 5))
    
    > (partner '(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 its first two arguments, and an initial value for the counter as its third argument.

    Note: We will discuss interface procedures in class on Thursday, in 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 auto-grader to find and run your code.