My pleasure, DEVITG
I guess it's not a bad example for practicing ones MAPCAR skills.
Take for instance the dot product of two vectors: P1 dot P2. It can be written out as (+ (* x1 x2)(* y1 y2)(* z1 z2)), or without having to assign six different variables:
(+ (* (car p1)(car p2))(* (cadr p1)(cadr p2))(* (caddr p1)(caddr p2)))
That's quite a lot to write. Cos' multiplications happen on same level in two point lists, it can more easily be written as (mapcar '* p1 p2). To add all values in the list of products, simply apply an addition:
(apply '+ (mapcar '* p1 p2))
And so, a dot product function was born.
Of course, in the point-to-perpendicular case we needed to substract vectors before multiplying the coordinates. Written in its entirety it would look something like this:
(+ (* (- (car p3) (car p1))(- (car p2) (car p1))) (* (- (cadr p3) (cadr p1))(- (cadr p2) (cadr p1))) (* (- (caddr p3) (caddr p1)) (- (caddr p2) (caddr p1))))
Like with the simple multiplication above, the exact same things happen on each level. Only thing changing is the operator, which means that '(* p1 p2) should be replaced with '(* (- p3 p1)(- p2 p1)).
If one takes to subtract vectors before multiplying them, it could be written with multiple MAPCAR's:
(apply '+ (mapcar '* (mapcar '- p3 p1)(mapcar '- p2 p1)))
Or it could be stuffed into a LAMBDA function, doing it all in one go:
(apply '+ (mapcar '(lambda (a b c)(* (- c a) (- b a))) p1 p2 p3))