lisp - 调用参数应为结构实例的函数

标签 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) (coord-y point2))))))

(defun square (x) (* x x))

最佳答案

Lisp 家族语言的美妙之处在于(相对)统一的语法。就像你调用函数 square 一样通过写作 (square n)*通过 (* n1 n2 ...) , 你调用 dist ,它有两个参数,(dist point1 point2) .在上下文中,这可能类似于:

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

关于lisp - 调用参数应为结构实例的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18949435/

相关文章:

javascript - 如何在 JavaScript 中实现 lambda/匿名函数

haskell - LISP 或 Haskell

lisp - 在 lisp 中加载文件

Lisp 函数 : union

lisp - 来自 On Lisp 的奇异引用列表示例

functional-programming - 以下函数式编程模式的正确术语是什么?

emacs - 口齿不清错误 : Origin 9614327 is not in the stack of NIL

package - Common Lisp 将包名称附加到宏中带引号的键中

lisp - Lisp 中的默认编程

使用 +、-、* 和/对表达式执行符号和数字运算的 LISP 函数