curry.lisp
(define-macro curry (lambda (fn) ((define curry0 (lambda (args body) ((if (nil? args)
(body)
(`(lambda ((,(car args))) (,(curry0 (cdr args) body
)))
))
)))
(if (procedure? (apply fn))
((define args (car (cdr fn)))
(define body (cdr (cdr fn)))
(curry0 args body)
)
(fn))
)))
应用
(define add3 (curry(lambda (x y z) (+ x y z))))
(((add3 1) 2) 3)
介绍
这条语句
(curry(lambda (x y z) (+ x y z)))
等价于上面这条语句
(lambda (x) (lambda (y) (lambda (z) ((+ x y z)))))
暂不反对这种写法
(define add30 (lambda (x y z) (+ x y z)))
(define add3 (curry add30))
若想反对能够用宏重写 define 语句