Question

Given the following code, what would by the syntax for calling the function dist?

(defstruct coord 
  x 
  y)

(defstruct line 
  (point1 :type coord) 
  (point2 :type coord) )


(defun dist (point1 point2)
  (sqrt (+ (square (- (coord-x point1) (coord-x point2)))
           (square (- (coord-y point1) (coord-y point2))))))

(defun square (x) (* x x))
Was it helpful?

Solution

The beautiful thing about languages in the Lisp family is the (relatively) uniform syntax. Just like you call the function square by writing (square n) or * by (* n1 n2 ...), you call dist, which takes two arguments with, (dist point1 point2). In context, this might be something like:

(let ((point1 (make-coord …))
      (point2 …))
  (dist point1 point2))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top