Homework 4:
Structurally Recursive Functions
Due: Monday, February 23, 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. -
...
reverseor any Racket function that converts a list argument to another datatype. Process the list one element at a time. -
... a
letexpression or an internaldefinein 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>)
<list-of-booleans> ::= ()
| (<boolean> . <list-of-booleans>)
<list> ::= ()
| (<any> . <list>)
Problems
-
Write a structurally recursive function named
(every? lob)that takes a list of booleanslobas its argument.every?returns#tif all of the values inlobare#t, and#fotherwise. For example:> (every? '(#t #t #t)) #t > (every? (map (lambda (n) (<= n 40)) '(26 37 41 25 12))) ; 41 is bigger than 40 #f -
Write a structurally recursive function named
(reject pred? lst)that takes two arguments, a one-argument predicatepred?and a listlstof values of the type accepted bypred?.rejectreturns a list containing all the items fromlstthat failpred?. For example:> (reject negative? '(1 2 -1 -2 3 -3 5 -4 4)) '(1 2 3 5 4)
-
Write a structurally recursive function named
(partner lst1 lst2)that takes as arguments two lists,lst1andlst2.partnerreturns a list of pairs, where each pair consists of the items at the corresponding positions inlst1andlst2. The process stops when either list runs out of items. For example:> (partner '(a b c d e) '(1 2 3 4 5)) '((a . 1) (b . 2) (c . 3) (d . 4) (e . 5)) > (partner '(a b c) '(1 2 3 4 5)) '((a . 1) (b . 2) (c . 3))
-
Write a structurally recursive function named
(cons-at-end v lst)that takes two arguments: any Racket valuevand a listlst.cons-at-endreturns a list containing all of the items inlstfollowed byv. For example:> (cons-at-end 'e '(a b c d)) '(a b c d e)
Be sure to think about the answer to return in the base case. -
Write a structurally recursive function named
(positions-of s los)that takes two arguments, a symbolsand a list of symbolslos.
positions-ofreturns a list containing the zero-based positions of all occurrences ofsinlos. For example:> (positions-of 'a '(a b a c d a e f g a h i j k)) '(0 2 5 9)
Makepositions-ofan 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.