-2

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))
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
JaminB
  • 778
  • 3
  • 9
  • 20
  • In the code you've shown, you're already calling functions `sqrt`, `+`, `square`, `-`, `coord-x`, `coord-y`, and `*`. Are you having some trouble calling `dist` in the same fashion? It's not really clear what you're asking, given that you've already got the rest of this code. – Joshua Taylor Sep 22 '13 at 22:51

1 Answers1

0

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))
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Thank you. For whatever reason this code would throw type errors when I ran it, but using setf method to globalize the variables and then calling as you described worked like a charm. – JaminB Sep 22 '13 at 22:56