curried.texi 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 2012 Free Software Foundation, Inc.
  4. @c See the file guile.texi for copying conditions.
  5. @node Curried Definitions
  6. @section Curried Definitions
  7. The macros in this section are provided by
  8. @lisp
  9. (use-modules (ice-9 curried-definitions))
  10. @end lisp
  11. @noindent
  12. and replace those provided by default.
  13. Prior to Guile 2.0, Guile provided a type of definition known colloquially
  14. as a ``curried definition''. The idea is to extend the syntax of
  15. @code{define} so that you can conveniently define procedures that return
  16. procedures, up to any desired depth.
  17. For example,
  18. @example
  19. (define ((foo x) y)
  20. (list x y))
  21. @end example
  22. is a convenience form of
  23. @example
  24. (define foo
  25. (lambda (x)
  26. (lambda (y)
  27. (list x y))))
  28. @end example
  29. @deffn {Scheme Syntax} define (@dots{} (name args @dots{}) @dots{}) body @dots{}
  30. @deffnx {Scheme Syntax} define* (@dots{} (name args @dots{}) @dots{}) body @dots{}
  31. @deffnx {Scheme Syntax} define-public (@dots{} (name args @dots{}) @dots{}) body @dots{}
  32. Create a top level variable @var{name} bound to the procedure with
  33. parameter list @var{args}. If @var{name} is itself a formal parameter
  34. list, then a higher order procedure is created using that
  35. formal-parameter list, and returning a procedure that has parameter list
  36. @var{args}. This nesting may occur to arbitrary depth.
  37. @code{define*} is similar but the formal parameter lists take additional
  38. options as described in @ref{lambda* and define*}. For example,
  39. @example
  40. (define* ((foo #:keys (bar 'baz) (quux 'zot)) frotz #:rest rest)
  41. (list bar quux frotz rest))
  42. ((foo #:quux 'foo) 1 2 3 4 5)
  43. @result{} (baz foo 1 (2 3 4 5))
  44. @end example
  45. @code{define-public} is similar to @code{define} but it also adds
  46. @var{name} to the list of exported bindings of the current module.
  47. @end deffn