Idiomdrottning’s homepage

define-curry

Update: Now part of brev-separate.


A friend suggested a define-curry macro and to me what immediately came to mind was something like this:

(define-curry (foo bar baz bax)
  (print baz)
  (+ bar baz bax))

(=
 (foo 100 20 3)
 ((foo 100) 20 3)
 ((foo 100 20) 3)
 ((foo) 100 20 3)
 (((foo) 100) 20 3)
 (((foo 100) 20) 3)
 ((((foo) 100) 20) 3))

Prints seven 20 and evals #t.

Here is the implementation:

(import miscmacros)

(define-syntax-rule
  (define-curry (name . args) . body)
  (define (name . friends)
    (if (= (length friends) (length 'args))
        (apply (lambda args . body)  friends)
        (lambda stragglers
          (apply name (append friends stragglers))))))

Bonus episode

Here is an functional curry combinator that’s arbitrary arity but not recursive. It’s a replacement for cut when every slot is at the end of the arg list.

(define (🍛 name . args)
  (lambda stragglers
    (apply name (append args stragglers))))

(= 54321
 ((🍛 + 1 20 300) 4000 50000)
 ((🍛 (🍛 + 1 20 300) 4000) 50000))

⇒ #t