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


6.10.6 Higher-Order Functions

As a functional programming language, Scheme allows the definition of higher-order functions, i.e., functions that take functions as arguments and/or return functions. Utilities to derive procedures from other procedures are provided and described below.

Scheme Procedure: const value

Return a procedure that accepts any number of arguments and returns value.

(procedure? (const 3))        ⇒ #t
((const 'hello))              ⇒ hello
((const 'hello) 'world)       ⇒ hello
Scheme Procedure: negate proc

Return a procedure with the same arity as proc that returns the not of proc’s result.

(procedure? (negate number?)) ⇒ #t
((negate odd?) 2)             ⇒ #t
((negate real?) 'dream)       ⇒ #t
((negate string-prefix?) "GNU" "GNU Guile")
                              ⇒ #f
(filter (negate number?) '(a 2 "b"))
                              ⇒ (a "b")
Scheme Procedure: compose proc1 proc2 …

Compose proc1 with the procedures proc2 … such that the last proc argument is applied first and proc1 last, and return the resulting procedure. The given procedures must have compatible arity.

(procedure? (compose 1+ 1-)) ⇒ #t
((compose sqrt 1+ 1+) 2)     ⇒ 2.0
((compose 1+ sqrt) 3)        ⇒ 2.73205080756888
(eq? (compose 1+) 1+)        ⇒ #t

((compose zip unzip2) '((1 2) (a b)))
                             ⇒ ((1 2) (a b))
Scheme Procedure: identity x

Return X.

Scheme Procedure: and=> value proc

When value is #f, return #f. Otherwise, return (proc value).