Question 3: Higher order functions [4]

Below we show specifications for two functions, validPoint and addPoints, and an implementation of function sumPoints.

   ; (validPoint point)
   ; ------------------
   ; returns true if point is a valid x,y point,
   ;     i.e. a list of two numbers,
   ; returns nil otherwise
   ; e.g. (validPoint '(10 2)) returns t
   ;      (validPoint '(x))   returns nil

; (addPoints point1 point2) ; ------------------------- ; if point1 is a valid point, (x1 y1), and ; point2 is a valid point, (x2 y2), ; then return the point (x1+x2, y1+y2) ; otherwise return nil ; e.g. (addPoints '(1 2) '(10 20)) returns (11 22)
; (sumPoints pointList) ; --------------------- (defun sumPoints (pointsList) (cond ((not (listp pointsList)) nil) ((null pointsList) nil) (t (reduce 'addPoints pointsList))))

Assuming our points list contained points (1 1) (2 5) and (-1 3) (in that order) show the results of each of the three calls below:

       ; call #1
       (mapcar 'validPoint pointsList)

       ; call #2
       (sumPoints pointsList)

       ; call #3
       (maplist 'sumPoints pointsList)