summaryrefslogtreecommitdiff
path: root/doc/ref/api-procedures.texi
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2010-12-16 15:14:33 +0100
committerLudovic Courtès <ludo@gnu.org>2010-12-16 23:45:23 +0100
commit18f06db925a67cb6b174a9210f7bed8adced4e00 (patch)
treea4ca1837512a3c5a0f179d4f38f740f4792af601 /doc/ref/api-procedures.texi
parent0bfba83a038ba03924eb82841407d387310181d5 (diff)
downloadguile-18f06db925a67cb6b174a9210f7bed8adced4e00.tar.gz
Add `compose', `negate', and `const'.
* module/ice-9/boot-9.scm (compose, negate, const): New procedures. * doc/ref/api-procedures.texi (Higher-Order Functions): New node. * test-suite/Makefile.am (SCM_TESTS): Add `tests/procs.test'. * test-suite/tests/procs.test: New file.
Diffstat (limited to 'doc/ref/api-procedures.texi')
-rw-r--r--doc/ref/api-procedures.texi57
1 files changed, 57 insertions, 0 deletions
diff --git a/doc/ref/api-procedures.texi b/doc/ref/api-procedures.texi
index 8fc7f33f5..c087f4c7e 100644
--- a/doc/ref/api-procedures.texi
+++ b/doc/ref/api-procedures.texi
@@ -13,6 +13,7 @@
* Compiled Procedures:: Scheme procedures can be compiled.
* Optional Arguments:: Handling keyword, optional and rest arguments.
* Case-lambda:: One function, multiple arities.
+* Higher-Order Functions:: Function that take or return functions.
* Procedure Properties:: Procedure properties and meta-information.
* Procedures with Setters:: Procedures with setters.
@end menu
@@ -573,6 +574,62 @@ arguments, and on the predicate; keyword arguments may be present but
do not contribute to the ``success'' of a match. In fact a bad keyword
argument list may cause an error to be raised.
+@node Higher-Order Functions
+@subsection Higher-Order Functions
+
+@cindex higher-order functions
+
+As a functional programming language, Scheme allows the definition of
+@dfn{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.
+
+@deffn {Scheme Procedure} const value
+Return a procedure that accepts any number of arguments and returns
+@var{value}.
+
+@lisp
+(procedure? (const 3)) @result{} #t
+((const 'hello)) @result{} hello
+((const 'hello) 'world) @result{} hello
+@end lisp
+@end deffn
+
+@deffn {Scheme Procedure} negate proc
+Return a procedure with the same arity as @var{proc} that returns the
+@code{not} of @var{proc}'s result.
+
+@lisp
+(procedure? (negate number?)) @result{} #t
+((negate odd?) 2) @result{} #t
+((negate real?) 'dream) @result{} #t
+((negate string-prefix?) "GNU" "GNU Guile")
+ @result{} #f
+(filter (negate number?) '(a 2 "b"))
+ @result{} (a "b")
+@end lisp
+@end deffn
+
+@deffn {Scheme Procedure} compose proc rest ...
+Compose @var{proc} with the procedures in @var{rest}, such that the last
+one in @var{rest} is applied first and @var{proc} last, and return the
+resulting procedure. The given procedures must have compatible arity.
+
+@lisp
+(procedure? (compose 1+ 1-)) @result{} #t
+((compose sqrt 1+ 1+) 2) @result{} 2.0
+((compose 1+ sqrt) 3) @result{} 2.73205080756888
+(eq? (compose 1+) 1+) @result{} #t
+
+((compose zip unzip2) '((1 2) (a b)))
+ @result{} ((1 2) (a b))
+@end lisp
+@end deffn
+
+@deffn {Scheme Procedure} identity x
+Return X.
+@end deffn
+
@node Procedure Properties
@subsection Procedure Properties and Meta-information