**Exercise 6.23**

A three-dimensional (3D) vector has x, y, and z coordinates, which are numbers. 3D vectors can be constructed and accessed using the following abstract interface:

(make-3D-vector x y z)

(x-coord vector)

(y-coord vector)

(z-coord vector)

**a.** Using this abstract interface, define procedures for adding two vectors, finding the dot-product of two vectors, and scaling a vector by a numerical scaling factor.

**b.** Choose a representation for vectors and implement make-3D-vector, x-coord, y-coord, and z-coord.

**Solution:** **a.**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
(define add-vectors (lambda (a b) (make-3D-vector (+ (x-coord a) (x-coord b)) (+ (y-coord a) (y-coord b)) (+ (z-coord a) (z-coord b))))) (define dot-vectors (lambda (a b) (+ (* (x-coord a) (x-coord b)) (* (y-coord a) (y-coord b)) (* (z-coord a) (z-coord b))))) (define scale-vector (lambda (a s) (make-3D-vector (* s (x-coord a)) (* s (y-coord a)) (* s (z-coord a))))) |

**b.**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
(define make-3D-vector (lambda (x y z) (cons x (cons y z)))) (define x-coord (lambda (a) (car a))) (define y-coord (lambda (a) (car (cdr a)))) (define z-coord (lambda (a) (cdr (cdr a)))) |

This was a quite short chapter. However chapter 7 is rather long. This exercise above covers pretty much the whole chapter, it was just about cons, car and cdr.