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
, andfilter
. -
...
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 internaldefine
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.
Note: Problem 5 does require you to write two functions, but neither is a helper function in the traditional sense.
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
-
Write a structurally recursive function named
(copy-to s los)
that takes that takes two arguments, a symbols
and a list of symbolslos
.
copy-to
returns a list containing all of the symbols inlos
up to the first occurrence ofs
. For example:> (copy-to 'b '(c o w b e l l)) '(c o w)
-
Write a structurally recursive function named
(collect f lon)
that takes two arguments, a function of one numberf
and a list of numberslon
.
collect
returns a list of itemsf(i)
for every i inlon
. For example:> (collect sqrt '(100 36 64 16 25 49 4)) ; sqrt finds the square root '(10 6 8 4 5 7 2)
-
Write a structurally recursive function named
(insert-before new-sym sym los)
that takes three arguments: two symbols,new-sym
ands
, and a list of symbolslos
.
insert-before
returns a list just likelos
, except withnew-sym
occurring before the first occurrence ofs
. For example:> (insert-before 'a 'b '(c b b i e)) '(c a b b i e)
-
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 inlon
passes thetest?
, 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 named
(positions-of s los)
that takes two arguments, a symbols
and a list of symbolslos
.
positions-of
returns a list containing the zero-based positions of all occurrences ofs
inlos
. For example:> (positions-of 'a '(a b a c d a e f g a h i j k)) '(0 2 5 9)
Makepositions-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:
-
homework04.rkt
, the source file containing your function definitions and test cases
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.