For Development HEAD DRAFTSearch (procedure/syntax/module):

Next: , Previous: , Up: Library modules - SRFIs   [Contents][Index]

11.61 srfi.232 - Flexible curried procedures

Module: srfi.232

This srfi provides macros to define a procedure that can accept less than or more than the given formal parameters. If the number of given parameters is less than the required parameters, it becomes partial application; a procedure that will take the remaining parameters is returned. If the number of given parameters is more than the maximum number of parameters, then the excessive parameters are passed to the result of the original procedure, assuming the original procedure returns a procedure.

Currying in the strict sense converts n-ary procedures to nested 1-ary procedures:

(lambda (a b c) body)
  ⇒ (lambda (a) (lambda (b) (lambda (c) body)))

However, with Scheme, you need nested parentheses to give all the parameters to it:

(((f 1) 2) 3)

The procedure created with this srfi allows more than one parameters to be passed at once. The following calls to f all yield the same result:

(define-curried (f a b c) body)

(f 1 2 3)
(((f 1) 2) 3)
((f 1 2) 3)
((f 1) 2 3)

Moreover, the procedure can take more parameters than the formals, assuming that the procedure with full parameters return a procedure that takes extra parameters. In other words, the following equivalance holds:

(f 1 2 3 4 5 6) ≡ ((f 1 2 3) 4 5 6)
Macro: curried formals body …

[SRFI-232]{srfi.232} Returns a procedure just like (lambda formals body …), except that the returned procedure may take less or more parameters than the specified in formals.

If it is given with less parameters than the required ones, it works as partial application; the result is a procedure that will accept the remaining parameters.

(define f (curried (a b c d) (+ a b c d)))

(f 1 2 3 4) ⇒ 10        ; ordinary application
(f 1 2) ⇒ #<procedure>  ; partial application, like (pa$ f 1 2)
((f 1 2) 3 4) ⇒ 10      ; fulfilling the remaining argument
(((f 1 2) 3) 4) ⇒ 10    ; partial application can be nested
(((f 1) 2 3) 4) ⇒ 10    ; ... and can be grouped in any way

The procedure can accept more parameters than the ones given to formals, even formals does not specify the “rest” parameter. If formals takes the rest parameter, the excess arguments are passed to it. If not, the result of the procedure with required parameters are applied over the excess arguments.

(define f (curried (a b c) (^[x] (* x (+ a b c)))))

(f 1 2 3 4) ⇒ 24  ; ≡ ((f 1 2 3) 4)

If no arguments are given to the procedure, it returls the procedure itself:

(((f)) 1 2 3) ≡ (f 1 2 3)

As a special case, (curried () body) and (curried identifier body) are the same as (lambda () body) and (lambda identifier body), respectively.

Macro: define-curried (name . formals) body …

[SRFI-232]{srfi.232} A shorthand of (define name (curried formals body …)).


Next: , Previous: , Up: Library modules - SRFIs   [Contents][Index]


For Development HEAD DRAFTSearch (procedure/syntax/module):
DRAFT