Created
December 27, 2013 05:16
-
-
Save Javran/8142839 to your computer and use it in GitHub Desktop.
a generic `curry` implementation in mit-scheme
This file contains hidden or 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
(define (curry f) | |
(define (curry-aux f args arity) | |
(lambda (a) | |
(let ((new-args (cons a args))) | |
(cond ((= arity 1) | |
(apply f (reverse! new-args))) | |
((> arity 1) | |
(curry-aux f new-args (- arity 1))) | |
; fail otherwise | |
)))) | |
; please refer to: | |
; http://www.gnu.org/software/mit-scheme/documentation/mit-scheme-ref/Arity.html | |
; for `procedure-arity` and `procedure-arity-min` | |
(curry-aux f '() (procedure-arity-min (procedure-arity f)))) | |
(define (test3 a b c) | |
(for-each | |
(lambda (x) (display x) (newline)) | |
(list a b c))) | |
(define (test9 a b c d e f g h i) | |
(for-each | |
(lambda (x) (display x) (newline)) | |
(list a b c d e f g h i))) | |
(test3 'a 'b 'c) | |
((((curry test3) 'a) 'b) 'c) | |
(test9 1 2 3 4 5 6 7 8 9) | |
(let loop ((f (curry test9)) | |
(i 1)) | |
(if (> i 9) | |
'done | |
(loop (f i) (+ i 1)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment