| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Gauche has some primitive procedures that allows combinatory programming.
(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.
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.
(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 |
#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)
|
#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] | [ ? ] |