如何更简单地表达lisp(lambda(pair)(应用#' f对))?

时间:2015-10-06 07:27:01

标签: lisp common-lisp partial

说我有以下功能:

(defun f (v1 v2) ...)

我已经获得了以下要简化的代码:

(lambda (pair) (apply #'f pair))

这里的目标是创建一个函数,该函数获取两个值的列表,并使用该列表的内容调用f。虽然这有效,但似乎有点冗长。是否有更简单的方式来表达这一点?

1 个答案:

答案 0 :(得分:3)

Maybe:

(lambda(p)(apply #'f p)) ;; 24 bytes

More seriously, Alexandria provides a curry function:

(let ((fun (curry #'apply #'f)))
  (funcall fun '(a b)))

Edit: Also, I agree with the sentiment expressed by @Paulo Madeira that your original version is far from being verbose. I would let it as it is, thought, because I think it is in fact easier to debug than using an assert.

(lambda (args) (apply #'f args))

... means "take a list of arguments and apply function F with those arguments". The meaning is straightforward and the function does not make assumptions about what F accepts as arguments. If F is later changed, your closure will still work reliably.

Edit: We don't know what is the context of your code and without more information, your closure looks fishy. If you could describe why you need to pass arguments, we might realize that this approach is not necessarily the most appropriate (or agree that this is in fact a sensible thing to do).