Paste number 36324: Currying macro

Paste number 36324: Currying macro
Pasted by: pjd
When:2 years, 4 months ago
Share:Tweet this! | http://paste.lisp.org/+S10
Channel:#scheme
Paste contents:
Raw Source | XML | Display As
(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)))))))

(define-syntax define-curried
  (syntax-rules ()
    ((define-curried (name args ...) body ...)
     (define name (curried (args ...) body ...)))))

(define-curried (mad a b c)
  (+ (* a b) c))

(mad 2 3 4)
((mad 2) 3 4)
((mad 2 3) 4)
(((mad 2) 3) 4)

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.