Math With More Curry

Jun 27, 2012 16:33

A StackOverflow poster asked if auto-currying functions could be implemented in Lisp dialects, and I decided to take a crack at it in Common Lisp.

Currying is easy enough to implement in Common Lisp, as shown here:
(defun curry (function &rest args)
(lambda (&rest more-args)
(apply function (append args more-args))))But I found my (hopefully correct) implementation of auto-currying rather amusingly self-referential:
(defun auto-curry (function num-args)
(lambda (&rest args)
(if (>= (length args) num-args)
(apply function args)
(auto-curry (apply (curry #'curry function) args)
(- num-args (length args))))))

programming, math

Previous post Next post
Up