Last active
January 12, 2016 00:50
-
-
Save PiDelport/6535964 to your computer and use it in GitHub Desktop.
This snippet defines curried versions of lambda and define, and was originally posted on #scheme (Freenode IRC, 2007)
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
;;; Analogous to lambda, but curried. | |
(define-syntax curried | |
(syntax-rules () | |
((curried () body ...) (lambda () body ...)) | |
((curried (arg) body ...) (lambda (arg) body ...)) | |
((curried (arg args ...) body ...) | |
(lambda (arg . rest) | |
(let ((next (curried (args ...) body ...))) | |
(if (null? rest) | |
next | |
(apply next rest))))))) | |
;;; Analogous to define, but curried. | |
(define-syntax define-curried | |
(syntax-rules () | |
((define-curried (name args ...) body ...) | |
(define name (curried (args ...) body ...))))) | |
;;; Sample usage: | |
;;; | |
;;; (define-curried (foo x y z) (+ x (/ y z))) ; foo has arity 3 | |
;;; | |
;;; ((foo 3) 1 2) ; (foo 3) is a procedure with arity 2 | |
;;; ((foo 3 1) 2) ; (foo 3 2) is a procedure with arity 1 |
Mirrored at: https://bitbucket.org/snippets/pjdelport/rkAB
Further work:
- "λ*: Beyond Currying", Jason Hemann, Daniel P. Friedman. Scheme and Functional Programming Workshop, 2013.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This inspired some blog posts that expand on the idea: