[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.15.1.2 Combinators

Gauche has some primitive procedures that allows combinatory programming.

Function: pa$ proc arg ...
Partial application. Returns a procedure, and when it is called with arguments m ..., it is equivalent to call (proc arg ... m ...).

 
(define add3 (pa$ + 3))
(add3 4) => 7

(map (pa$ * 2) '(1 2 3)) => (2 4 6)

Macros cut and cute defined in SRFI-26 provide a similar abstraction, with a bit more flexible but less compact notation. See section 4.3 Making Procedures.

Function: apply$ proc
Function: map$ proc
Function: for-each$ proc
Partial application version of apply, map and for-each.

 
(define map2* (map$ (pa$ * 2)))
(map2* '(1 2 3)) => (2 4 6)

There are more 'partial-applied' version of procedures defined in SRFI-1 (See section 10.2 srfi-1 - List library), such as fold$, filter$, member$, etc.

Function: compose f g h ...
Combine two or more procedures. Arguments must be procedures. When two procedures are given, (compose f g) is equivalent to
 
(lambda args (call-with-values (lambda () (apply g args)) f))

Some examples:

 
(define not-zero? (compose not zero?))
(not-zero? 3) => #t
(not-zero? 0) => #f

(define dot-product (compose (apply$ +) (map$ *)))
(dot-product '(1 2 3) '(4 5 6)) => 32

Function: any-pred pred ...
Returns a procedure which applies given argument(s) to each predicate pred. If any pred returns a non-#f value, the value is returned. If all the preds return #f, #f is returned.

 
(define string-or-symbol? (any-pred string? symbol?))
(string-or-symbol? "abc") => #t
(string-or-symbol? 'abc)  => #t
(string-or-symbol? 3)     => #f

(define <> (any-pred < >))
(<> 3 4) => #t
(<> 3 3) => #f

((any-pred (cut memq <> '(a b c))
           (cut memq <> '(1 2 3)))
 'b)  => '(b c)

Function: every-pred pred ...
Returns a procedure which applies given argument(s) to each predicate pred. If every pred returns a non-#f value, the value returned by the last pred is returned. If any pred returns #f, every-pred returns #f without calling further preds.

 
((every-pred odd? positive?) 3)  => #t
((every-pred odd? positive?) 4)  => #f
((every-pred odd? positive?) -3) => #f

(define safe-length (every-pred list? length))
(safe-length '(a b c))  => 3
(safe-length "aaa")     => #f


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Ken Dickey on November, 28 2002 using texi2html