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...
- ... any of Racket's primitive higher-order functions
in any of your solutions, including map,
apply, and filter.
- ... any function that converts a list argument to
another datatype. Process the list.
- ... a let expression or an internal
define in any of your solutions.
- 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?,
check-=, or check-true/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.
Problems
- 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.
- 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)
- 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)
- 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
- 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:
- homework04.rkt, the source file containing your
function definitions and test cases
Be sure that your submission follows the
submission requirements.
Eugene Wallingford .....
wallingf@cs.uni.edu .....
February 16, 2023