diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2023-08-14 15:08:00 +0200 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2023-08-15 00:33:50 +0200 |
| commit | d20a7da4a38e96ded5bda1aab2df75f2eee2af85 (patch) | |
| tree | df17f9eaaa6a32bab374ed8ea1f367c2ce7c5d08 /doc | |
| parent | 3c7d465133d56737c4209dcf25370f98da88a30b (diff) | |
doc: cookbook: Mention common SRFI-1 procedures.
* doc/guix-cookbook.texi (A Scheme Crash Course): Add item about
SRFI-1.
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/guix-cookbook.texi | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/doc/guix-cookbook.texi b/doc/guix-cookbook.texi index 28303df37b2..ee66dad7d9b 100644 --- a/doc/guix-cookbook.texi +++ b/doc/guix-cookbook.texi | |||
| @@ -192,7 +192,8 @@ rest are the arguments passed to the function. Every function returns the | |||
| 192 | last evaluated expression as its return value. | 192 | last evaluated expression as its return value. |
| 193 | 193 | ||
| 194 | @item | 194 | @item |
| 195 | Anonymous functions are declared with the @code{lambda} term: | 195 | Anonymous functions---@dfn{procedures} in Scheme parlance---are declared |
| 196 | with the @code{lambda} term: | ||
| 196 | 197 | ||
| 197 | @lisp | 198 | @lisp |
| 198 | (lambda (x) (* x x)) | 199 | (lambda (x) (* x x)) |
| @@ -208,6 +209,9 @@ which can in turn be applied to an argument: | |||
| 208 | @result{} 9 | 209 | @result{} 9 |
| 209 | @end lisp | 210 | @end lisp |
| 210 | 211 | ||
| 212 | Procedures are regular values just like numbers, strings, Booleans, and | ||
| 213 | so on. | ||
| 214 | |||
| 211 | @item | 215 | @item |
| 212 | Anything can be assigned a global name with @code{define}: | 216 | Anything can be assigned a global name with @code{define}: |
| 213 | 217 | ||
| @@ -234,6 +238,30 @@ A list structure can be created with the @code{list} procedure: | |||
| 234 | @end lisp | 238 | @end lisp |
| 235 | 239 | ||
| 236 | @item | 240 | @item |
| 241 | Standard procedures are provided by the @code{(srfi srfi-1)} module to | ||
| 242 | create and process lists (@pxref{SRFI-1, list processing,, guile, GNU | ||
| 243 | Guile Reference Manual}). Here are some of the most useful ones in | ||
| 244 | action: | ||
| 245 | |||
| 246 | @lisp | ||
| 247 | (use-modules (srfi srfi-1)) ;import list processing procedures | ||
| 248 | |||
| 249 | (append (list 1 2) (list 3 4)) | ||
| 250 | @result{} (1 2 3 4) | ||
| 251 | |||
| 252 | (map (lambda (x) (* x x)) (list 1 2 3 4)) | ||
| 253 | @result{} (1 4 9 16) | ||
| 254 | |||
| 255 | (delete 3 (list 1 2 3 4)) @result{} (1 2 4) | ||
| 256 | (filter odd? (list 1 2 3 4)) @result{} (1 3) | ||
| 257 | (remove even? (list 1 2 3 4)) @result{} (1 3) | ||
| 258 | (find number? (list "a" 42 "b")) @result{} 42 | ||
| 259 | @end lisp | ||
| 260 | |||
| 261 | Notice how the first argument to @code{map}, @code{filter}, | ||
| 262 | @code{remove}, and @code{find} is a procedure! | ||
| 263 | |||
| 264 | @item | ||
| 237 | @cindex S-expression | 265 | @cindex S-expression |
| 238 | The @dfn{quote} disables evaluation of a parenthesized expression, also | 266 | The @dfn{quote} disables evaluation of a parenthesized expression, also |
| 239 | called an S-expression or ``s-exp'': the first term is not called over | 267 | called an S-expression or ``s-exp'': the first term is not called over |
