The goals for this assignment are increasing your skills with
- Writing Scheme functions
- Using recursion with arbitrary lists
- Getting some practice with Map and Apply
- Using Scheme for some basic mathematical operations with vectors and matrices.
You can use the solution to any exercise as a helper function in subsequent exercises.
Assume the following data types for input to the functions:
|a, b, c ...
||atoms (numbers or symbols)
|n, m, ...
||A list of atoms
||A list of any kind
||A list of numbers (a vector)
||A matrix -- a list of lists, all of the same length, which make up the rows of the matrix. In this lab the matrix entries will be numbers.
- Write (firsts llyst) and (rests llyst). For both of these llyst is a list of lists. (firsts llyst) returns a new list with the cars of each element of llyst; (rests llyst) returns a new list with the cdrs of each element of llyst.
- (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) )
- Write (addvec vec1 vec2), where vec1 and vec2 are vectors (lists of numbers) with the same length. This returns a vector containing the sums of the corresponding elements of vec1 and vec2.
- (addvec '(1 2 3) '(4 5 6) ) returns (5 7 9)
- (addvec '( ) '( ) ) returrns ( )
- Write (dot-product vec1 vec2), where again vec1 and vec2 are vectors with the same length. This returns the sum of the products of the corresponding elements of vec1 and vec2.
- (dot-product '(1 2 3) '(4 5 6) ) returns 32 (= 1*4 + 2*5 + 3*6)
- (dot-product '( ) '( )) returns 0
- Write (dot-row vec mat), 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.
- (dot-row '(1 2 3) '((1 4 7) (2 5 8) (3 6 9)) ) returns (30 36 42)
- Write (transpose mat), which returns the transpose of mat. This interchanges the rows and the columns of mat, i.e., the first row of mat is the first column of the transpose, the second row of mat becomes the second column of the transpose, and so forth.
Hint: use firsts and rests.
- (transpose '((1 2 3) (4 5 6)) ) returns ((1 4) (2 5) (3 6) )
- Write (matmult mat1, mat2), which returns the matrix product of mat1 and mat2. The entry of the ith row and jth column of the product is the dot-product of row i of mat1 and column j of mat2. For this to make sense the lengths of the rows of mat1 must be the same as the lengths of the columns of mat2. For example,
- (matmult '((1 2 3) (4 5 6)) '((1 2) (3 4) (5 6)) ) returns ((22 28) (49 64))
Hint: use a helper function that works with mat1 and the transpose of mat2. This helper function makes a lot of use of dot-row.
- Write (flatten L) that takes a list L and creates a flat list wth the same elements.
- (flatten '(x y z z y) ) returns(x y z z y)
- (flatten '(a (x (y)) (((y)) y z)) returns (a x y y y z)
- Consider a list, not necessarily flat, of numbers, such as ( (1 (2)) (((4))) 5). Use map and apply to write a procedure (sum L) that sums all of the numbers in such a list.
- Again consider a general list of numbers and a function f of one argument. Use map and apply to write a procedure (apply-to f L) that builds a new list with the same structure as L, only f is applied to each of the elements of L to get the values in the new list. For example, (apply-to add1 '(3 (4 5))) is (4 (5 6)).
- Write the procedure (element-of? a L) that returns true if atom a is an element of (not-necessarily flat) list L.