Skip to content

Instantly share code, notes, and snippets.

@PiDelport
Last active January 12, 2016 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PiDelport/6535964 to your computer and use it in GitHub Desktop.
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)
;;; 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
@PiDelport
Copy link
Author

This inspired some blog posts that expand on the idea:

@PiDelport
Copy link
Author

@PiDelport
Copy link
Author

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