我使用一些变量定义了多个函数,这些变量通常会在更多变量中进行计算。因此,我使用关键字参数,默认情况下计算所需的值。
示例:
(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/