function - 使用相同变量定义函数的最优雅/有效的方法

标签 function common-lisp redundancy keyword-argument

我使用一些变量定义了多个函数,这些变量通常会在更多变量中进行计算。因此,我使用关键字参数,默认情况下计算所需的值。

示例:

(defun f (a b &key (distance (distance a b))) (c a b distance))
(defun g (a b &key (distance (distance a b))) (if (< distance (r a b))
                                                (f a b :distance distance)))
(defun h (a b &key (distance (distance a b))) (d a b distance))
(defun i (a b &key (distance (distance a b))) (g a b :distance distance)
                                              (h a b :distance distance))

每个函数都应该能够单独调用,而无需指定关键字参数。

但现在我还必须在每个函数中定义关键字参数的计算。

是否有更优雅/更有效的方法来完成这样的事情? (我想到了惰性求值和动态编程)

最佳答案

您可以使用#=:

(defun foo (a b &key #1=(distance (distance a b))) #|...|#)

(defun bar (a b &key #1#) #|... |#)

关于function - 使用相同变量定义函数的最优雅/有效的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29327637/

相关文章:

c# - 如何将 xsd 转换为具有冗余的 cs? (OTA XML 出版物)

c++ - 使用 reed solomon 完全恢复数据

php - 为什么我无法将连接变量传递给我的函数?

c++ - C++ 函数中的复杂参数 - 指针、引用或...?

common-lisp - common-lisp中有关可选参数的错误

lisp - ' ('(LIST) ' 无 'NIL) should be a lambda expression in (hanoi(' ('(list)' ()'())))

lisp - Common Lisp 并行采样

function - 初始化函数字段

c - *.hxx 中声明的 void 函数,在 *.cxx 中定义但在 *.c 中未定义

email - 我可以将 Google Apps 用作故障转移邮件服务器吗?