Homework Assignment 4

Structurally Recursive Functions

CS 3540
Programming Languages and Paradigms
Spring Semester 2018

Due: Tuesday, February 13 at 8:00 AM


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. For this assignment:

Template Source File
Download the file homework04.rkt and use it as a template for your submission. Please use the name homework04.rkt for your file.

This file includes a provide clause that exports your five public functions. This enables me to load your module and test your functions using my own Rackunit tests.
Do Not Use...
Organizing Code


  1. Write a recursive function (string* str n) that takes two arguments: a string x and a non-negative integer n. string* returns a string consisting of n occurrences of str. For example:
         > (string* "Eugene" 3)
    You will want to use Racket's string-append function. Remember that the base case when recurring on a non-negative integer is 0.

  2. Write a structurally recursive function (reject test? lst) that takes two arguments, a one-argument function test? and a list lst. reject returns a list of all items in lst that fail the test?. For example:
         > (reject positive? '(1 2 -1 -2 3 -3 5 -4 4))
         '(-1 -2 -3 -4)
  3. Write a structurally recursive function (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. For example:
         > (interleave '(a b c d e) '(1 2 3 4 5))
         '((a . 1) (b . 2) (c . 3) (d . 4) (e . 5))
    You may assume that lst1 and lst2 are the same length. You'll receive an extra credit point if your function handles lists of unequal lengths gracefully.

  4. Write a structurally recursive function (every? lob) that takes a list of booleans lob as its argument. every? returns #t if all of the booleans in lob are #t, and #f otherwise. For example:
         > (every? (map (lambda (n) (<= n 40))
                        '(26 37 41 25 12)))

  5. Write a structurally recursive function (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.


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 7, 2018