-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise2-37.lisp
More file actions
32 lines (30 loc) · 934 Bytes
/
exercise2-37.lisp
File metadata and controls
32 lines (30 loc) · 934 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(define accumulate foldr)
(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 mat)
(accumulate-n cons '() mat))
(define (matrix-*-matrix m n)
(map (lambda (row)
(map (lambda (col)
(dot-product row col))
(transpose n)))
m))
(define my-matrix (list (list 1 2 3)
(list 4 5 6)
(list 7 8 9)))
(define my-vector (list 10 11 12))
(display (dot-product my-vector my-vector))
(newline)
(display (matrix-*-vector my-matrix my-vector))
(newline)
(display (transpose my-matrix))
(newline)
(display (matrix-*-matrix my-matrix my-matrix))
(newline)