2013-09-22 31 views
-2

給定以下代碼,調用函數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)) 
+0

在你已經證明,你已經調用函數'sqrt','+','square','-'代碼,'座標,x' ,'coord-y'和'*'。以同樣的方式調用'dist'有問題嗎?由於你已經得到了這段代碼的其餘部分,所以你不清楚你在問什麼。 –

回答

0

Lisp系列中語言的優點是(相對)統一的語法。就像您通過書寫(square n)*(* n1 n2 ...)調用函數square一樣,您可以調用dist,它帶有(dist point1 point2)兩個參數。在上下文中,這可能是這樣的:

(let ((point1 (make-coord …)) 
     (point2 …)) 
    (dist point1 point2)) 
+0

謝謝。無論出於何種原因,這段代碼在運行時會拋出類型錯誤,但是使用setf方法來全局化變量,然後像你所描述的那樣調用就像一個魅力。 – JaminB

相關問題