Languages like Fortran and APL have special operators that take several operator
ID: 3624105 • Letter: L
Question
Languages like Fortran and APL have special operators that take several operators as operands. One of these isthe inner product operator that takes two arguments, either vectors or matrixes and multiplies the corresponding
elements of two arguments, and then sums the results. Write a Scheme procedure vectorproduct the takes two
lists and returns the output of inner product operator. If one of the lists is empty, the function should return a 0.
(vectorproduct '(2 3 4 ) '( 5 6 7) ) => 56
(vectorproduct '( ) '( 5 6 7) ) => 0
Explanation / Answer
(define (vectorproduct x y) (if (equal? x '()) 0
(if (equal? y '()) 0
(+ (* (car x) (car y))
(vectorproduct (cdr x) (cdr y))))))
Explanation (by example):
If you know the vectorproduct of '(3 4) and '(6 7) (i.e. you know what 3*6+4*7 equals) then you can get the vectorproduct of '(2 3 4) '(5 6 7) by adding 2*5 to the vectorproduct of '(3 4) and '(6 7).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.