0
votes

I'm trying to write a matrix multiplication function in scheme for a school assignment, but I'm not allowed to use functions we haven't talked about yet, such as map and lambda (outside of defining function parameters). I've already written a function that can multiply a vector by a matrix and a function that can take the dot product of two vectors. For simplicity's sake I'm allowed to assume that the inputs will be valid.

So, the function would take two matrices as parameters. I know the row vectors would be the elements of the first matrix, but how do I get at the columns of the second matrix?

Here's my vector multiplication function:

(define vectormult
(lambda (vec mat)
 (cond
((null? mat) vec)     
(else (cons (dotproduct vec (car mat)) (vectormult vec (cdr mat)))))))

Here's my dot product function:

(define dotproduct
  (lambda(l1 l2)
    (cond
      ((null? l1) 0)
      ((null? l2) 0)
      (else (+ (* (car l1) (car l2)) (dotproduct (cdr l1) (cdr l2)))))))

So how do I iterate through both matrices using recursion?

1

1 Answers

0
votes

I actually figured this out. First of all, my vectormult function was multiplying the vectors in the wrong order. I needed helper functions in order to extract the right columns to multiply the vector by. My final code calls vectormult and does recursion on the cdr of the first matrix.

(define matrixmultiply
(lambda (m1 m2)
(cond
  ((null? m2) '())
  ((null? (car m2)) '())
  (else (vectormult (car m1) m2) (matrixmultiply (cdr m1) m2)))))