The goal for this assignment is to practice using
apply wherever you can to implement the functions. If you can’t see how to use
apply, I’ll accept other ways of implementing the functions, but do make an effort to use the higher-order functions.
Your implementations of the following functions should all be placed in a single Racket file named
hw3.rkt. The corresponding tests for each function should be in a second Racket file named
You may use the solution to any exercise as a helper function in subsequent exercises and you may also write stand-alone helper functions.
The start of each file should be
; Your name(s) here.
Click on the assignment link. If you’re working with a partner, one partner should create a new team. The second partner should click the link and choose the appropriate team. It’s extremely helpful for the graders if you include your name and your partner’s name in the team name. Unfortunately, you cannot use the same team name for multiple assignments. You can append the homework number to your team name if you wish. (Please don’t choose the wrong team, there’s a maximum of two people and if you join the wrong one, you’ll prevent the correct person from joining.)
Once you have accepted the assignment and created/joined a team, you can clone the repository on your computer by following the instruction and begin working. But before you do, read the entire assignment and be sure to check out the expected coding style from the first homework.
Be sure to ask any questions on Piazza.
To submit your homework, you must commit and push to GitHub before the deadline.
Your repository should contain the following files
It may also a
.gitignore file which tells Git to ignore files matching patterns in your working directory.
Any additional files you have added to your repository should be removed from the
main branch. (You’re free to make other branches, if you desire, but make sure
main contains the version of the code you want graded.)
Make sure you put your name (and your partner’s name if you’re working with one) as a comment at the top of each file.
Part 1. Vectors and matrices as lists
For each function, write the function and a test suite and add the test suite to the
all-tests test suite. You might want to write the tests first. If you’ve forgotten how to write tests, consult homework 1.
Make sure you write at least two tests for each function. Make sure you test different cases.
Try to use
apply for each of your solutions. (The procedures are each very short when you do.)
(firsts lsts) and
(rests lsts). For both of these,
lsts is a list of lists.
(firsts lsts) returns a new list with the first element (the
first of each list) of each element of
(rests lsts) returns a new list with the remainder of each list (the
rest of each list).
(firsts '((a b c) (d e f) (g h i))) returns
'(a d g)
(rests '((a b c) (d e f) (g h i))) returns
'((b c) (e f) (h i))
(vec-+ vec1 vec2) where
vec2 are vectors (lists of numbers) with the same length. This returns the vector containing the sums of the corresponding elements of
(vec-+ '(1 2 3) '(4 5 6)) returns
'(5 7 9)
(vec-+ empty empty) returns
(dot-product vec1 vec2) where
vec2 are again vectors with the same length. This returns the dot product of the two vectors. That is, it’s the sum of product of corresponding elements in the two vectors.
(dot-product '(1 2 3) '(4 5 6)) returns
32 (which is )
(dot-product empty empty) returns 0
- We can represent a matrix by a list of vectors where each vector has the same length and represents one row of the matrix. For example
'((1 4 7)
(2 5 8)
(3 6 9))
represents the matrix
(mat-vec-* mat vec) where the length of
vec is the same as the length of each row of
mat. This returns a vector containing the dot product of
vec with each row of
(mat-vec-* '((1 4 7) (2 5 8) (3 6 9)) '(1 2 3)) returns
'(30 36 42)
(mat-vec-* '((2 3 4) (1 1 1)) '(1 0 1)) returns
The transpose of a matrix interchanges its rows and columns. I.e., the first row of the matrix is the first column of the transpose, the second row of the matrix is the second column of the transpose and so forth. For example, the transpose of is
(transpose mat) which returns the transpose of the matrix
mat. [Hint: Either use
rests from question (1) or use
list in a clever fashion. The clever solution is tricky, but running
(map list '(1 2) '(3 4)) might give a clue.]
(transpose '((1 4 7) (2 5 8) (3 6 9))) returns
'((1 2 3) (4 5 6) (7 8 9))
(transpose '((1 2 3) (4 5 6))) returns
'((1 4) (2 5) (3 6))
You have probably seen matrix multiplication before. The entry of the th row and th column of the product is the dot product of the th row of the first matrix and the th column of the second matrix. For this to make sense, the lengths of the rows of the first matrix must be the same as the lengths of the columns of the second. For example,
(mat-mat-* lhs rhs) that returns the product of matrices . [Hint: You may wish to use a helper function that works with
lhs and the transpose of
rhs and use
(mat-mat-* '((1 0 1) (2 1 1)) '((1 2) (1 0) (1 1))) returns
((2 3) (4 5))