Homework 4:
Structurally Recursive Functions

Due: Monday, February 24, 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>)
  

Problems

  1. Write a structurally recursive function named (copy-to s los) that takes that takes two arguments, a symbol s and a list of symbols los.

    copy-to returns a list containing all of the symbols in los up to the first occurrence of s. For example:
    > (copy-to 'b '(c o w b e l l))
    '(c o w)
    

  2. Write a structurally recursive function named (collect f lon) that takes two arguments, a function of one number f and a list of numbers lon.

    collect returns a list of items f(i) for every i in lon. For example:
    > (collect sqrt '(100 36 64 16 25 49 4))   ; sqrt finds the square root
    '(10 6 8 4 5 7 2)
    

  3. Write a structurally recursive function named (insert-before new-sym sym los) that takes three arguments: two symbols, new-sym and s, and a list of symbols los.

    insert-before returns a list just like los, except with new-sym occurring before the first occurrence of s. For example:
    > (insert-before 'a 'b '(c b b i e))
    '(c a b b i e)
    

  4. Write a structurally recursive function named (any? test? lon) that takes two arguments: a function of one number, test?, and list of numbers, lon.

    any? returns #t if any number in lon passes the test?, and #f otherwise. For example:
    > (any? negative? '(26 37 41 25 12))
    #f
    > (any? even? '(37 41 25 26 12))
    #t
    

  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.