Lisp 调用其参数应为结构实例的函数
给定以下代码,调用函数Lisp 调用其参数应为结构实例的函数,lisp,common-lisp,clisp,Lisp,Common Lisp,Clisp,给定以下代码,调用函数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) (co
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))
Lisp家族中语言的美妙之处在于(相对)统一的语法。就像您通过编写
(square n)
或*
通过(*n1n2…
)调用函数square
,您调用dist
,它使用两个参数,(dist point1 point2)
。在上下文中,这可能类似于:
(let ((point1 (make-coord …))
(point2 …))
(dist point1 point2))
在您显示的代码中,您已经在调用函数
sqrt
,+
,square
,-
,coord-x
,coord-y
,和*
。您是否在以相同方式调用dist
时遇到问题?考虑到你已经掌握了这段代码的其余部分,现在还不清楚你在问什么。谢谢。不管出于什么原因,我运行这段代码时会抛出类型错误,但使用setf方法来全球化变量,然后按照您所描述的那样调用,效果非常好。