Previous: , Up: Procedures   [Contents][Index]


11.5 Partial application

Syntax: cut slot-or-expr slot-or-expr* [<...>]

where each slot-or-expr is either an expression or the literal symbol <>.

It is frequently necessary to specialize some of the parameters of a multi-parameter procedure. For example, from the binary operation cons one might want to obtain the unary operation (lambda (x) (cons 1 x)). This specialization of parameters is also known as partial application, operator section, or projection. The macro cut specializes some of the parameters of its first argument. The parameters that are to show up as formal variables of the result are indicated by the symbol <>, pronouced as "slot". In addition, the symbol <...>, pronounced as "rest-slot", matches all residual arguments of a variable argument procedure.

A cut-expression is transformed into a lambda expression with as many formal variables as there are slots in the list slot-or-expr*. The body of the resulting lambda expression calls the first slot-or-expr with arguments from the slot-or-expr* list in the order they appear. In case there is a rest-slot symbol, the resulting procedure is also of variable arity, and the body calls the first slot-or-expr with remaining arguments provided to the actual call of the specialized procedure.

Here are some examples:

(cut cons (+ a 1) <>) is the same as (lambda (x2) (cons (+ a 1) x2))

(cut list 1 <> 3 <> 5) is the same as (lambda (x2 x4) (list 1 x2 3 x4 5))

(cut list) is the same as (lambda () (list))

(cut list 1 <> 3 <...>) is the same as (lambda (x2 . xs) (apply list 1 x2 3 xs))

The first argument can also be a slot, as one should expect in Scheme: (cut <> a b) is the same as (lambda (f) (f a b))

Syntax: cute slot-or-expr slot-or-expr* [<...>]

The macro cute (a mnemonic for "cut with evaluated non-slots") is similar to cut, but it evaluates the non-slot expressions at the time the procedure is specialized, not at the time the specialized procedure is called.

For example (cute cons (+ a 1) <>) is the same as (let ((a1 (+ a 1))) (lambda (x2) (cons a1 x2)))

As you see from comparing this example with the first example above, the cute-variant will evaluate (+ a 1) once, while the cut-variant will evaluate it during every invocation of the resulting procedure.


Previous: , Up: Procedures   [Contents][Index]