Last active
September 27, 2016 07:41
-
-
Save spacebat/a54cd2f90463a09b70d75da5df47df53 to your computer and use it in GitHub Desktop.
Curry with uncurry in common lisp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defvar *uncurry* nil "When bound and non-nil, curried functions can be uncurried") | |
(makunbound '*uncurry*) | |
(defun curry (function &rest args) | |
(lambda (&rest more-args) | |
(if (and (boundp '*uncurry*) *uncurry*) | |
(if more-args | |
(error "No more args expected") | |
function) | |
(apply function (append args more-args))))) | |
(defun uncurry (function) | |
(let ((*uncurry* t)) | |
(funcall function))) | |
(funcall (curry '+ 1) 2) | |
;; 3 | |
(uncurry (curry '+ 1)) | |
;; + | |
(uncurry (curry (lambda (x) (+ 1 x)) 1)) | |
;; #<anonymous interpreted function 4050012934> | |
(funcall (uncurry (curry (lambda (x) (+ 1 x)) 1)) 2) | |
;; 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment