Homework Assignment 4

Structurally Recursive Functions


CS 3540
Programming Languages and Paradigms
Spring Semester 2023


Due: Tuesday, February 21 at 10:00 AM


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

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



Problems

  1. Write a recursive function (string* str n) that takes two arguments: a string x and a non-negative integer n. The non-negative integers are defined inductively as:
         <number> ::= 0
                    | <number> + 1
    
    string* returns a string consisting of n occurrences of str. For example:
         > (string* "Eugene" 3)
         "EugeneEugeneEugene"
    
    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 (collect f lon) that takes two arguments, a one-argument function f and a list of numbers lon.
         <list-of-numbers> ::= ()
                             | (<number> . <list-of-numbers>)
    
    collect returns a list of items f(i) for every i in lon. For example:
         > (collect sqr '(1 2 -1 -2 3 -3 5 -4 4))   ; sqr squares its arg
         '(1 4 1 4 9 9 25 16 16)
    
  3. Write a structurally recursive function (insert-before new-sym sym los) that takes as arguments two symbols, new-sym and sym, and a list of symbols, los.
         <list-of-symbols> ::= ()
                             | (<synbol> . <list-of-symbols>)
    
    insert-before returns a list just like los, except with new-sym occurring before the first occurrence of sym. For example:
         > (insert-before 'a 'b '(c b b i e))
         '(c a b b i e)
    
  4. Write a structurally recursive function (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 (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.


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 16, 2023