Completed up to 2.37

This commit is contained in:
Oliver Payne 2021-10-28 22:29:07 +01:00
parent 73c2860a36
commit 478ae538a9
1 changed files with 28 additions and 0 deletions

28
2_37.sch Normal file
View File

@ -0,0 +1,28 @@
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
(define (accumulate-n op init seqs)
(if (null? (car seqs))
'()
(cons (accumulate op init (map car seqs))
(accumulate-n op init (map cdr seqs)))))
(define (dot-product v w)
(accumulate + 0 (map * v w)))
(define (matrix-*-vector m v)
(map (lambda (row) (dot-product row v)) m))
(define (transpose m)
(accumulate-n cons '() m))
(define (matrix-*-matrix m n)
(let ((cols (transpose n)))
(map (lambda (row)
(map (lambda (col)
(dot-product row col))
cols))
m)))