
1: @node Library modules - SRFIs, Library modules - Utilities, Library modules - Gauche extensions, Top 2: @chapter Library modules - SRFIs 3: @c NODE ?饤?Ö¥??⥸?塼?? SRFI 4: 5: @c EN 6: This chapter lists modules that provides SRFI functionalities. 7: Note that some of SRFI features are built in Gauche core and 8: not listed here. @xref{Standard conformance}, for entire list 9: of supported SRFIs. 10: @c JP 11: ???ξϤǤ?RFI?ε?ǽ??????????塼???ꥹ?Ȥ??Þ¤??? 12: ????RFI??auche?Î¥?????Ȥ߹??Þ¤????ê¡¢ 13: ???ξϤˤϵ󤲤??Ƥ??ʤ????Ȥ?????Ʋ??????? 14: ???Ý¡??Ȥ????????Ƥ?RFI?Υꥹ?Ȥ?ref{Standard conformance}?ˤ??????? 15: @c COMMON 16: 17: @c ---------------------------------------------------------------------- 18: @menu 19: * Feature conditional:: 20: * List library:: 21: * Homogeneous vectors:: 22: * A compatible let form with signatures and rest arguments:: 23: * Feature-based program configuration language:: 24: * Record types:: 25: * Let-values:: 26: * String library:: 27: * Character-set library:: 28: * Time data types and procedures:: 29: * Sources of random bits:: 30: * Localization:: 31: * A program argument processor:: 32: * Eager comprehensions:: 33: * Vector library:: 34: * Requiring extensions:: 35: @end menu 36: 37: @node Feature conditional, List library, Library modules - SRFIs, Library modules - SRFIs 38: @section @code{srfi-0} - Feature conditional 39: @c NODE ??ǽ???, @code{srfi-0} - ??ǽ??? 40: 41: @deftp {Module} srfi-0 42: @mdindex srfi-0 43: @c EN 44: Provides SRFI-0 @code{cond-expand} macro form. 45: Now this module is autoloaded when @code{cond-expand} is used, 46: so you don't need to say @code{(use srfi-0)} explicitly. 47: Portable programs can't use 'use' form anyway. 48: @c JP 49: ???Υ⥸?塼?? SRFI-0 ??@code{cond-expand} ?Þ¥???????????Þ¤??? 50: ???ß¡????Υ⥸?塼?? @code{cond-expand} ??Ѥ????Ȥ??Ë¡???ưŪ???í¡¼?ɤ??????Τǡ????Ū??@code{(use srfi-0)} ???ɬ??Ϥ????????Ý¡????Ö¥??×¥í¥°???ˤ????Ï¡?'use' ???????????????Ǥ??礦?? 51: @c COMMON 52: @end deftp 53: 54: @defmac cond-expand (feature-requirement command-or-definition @dots{}) @dots{} 55: [SRFI-0] 56: @c EN 57: This macro expands to @var{command-or-definition} @dots{} if Gauche 58: supports @var{feature-requirement}. 59: In a way, it can be used to absorb the different feature sets supported by 60: different Scheme implementations. 61: 62: @var{feature-requirement} must be in the following syntax: 63: @c JP 64: Gauche ?? @var{feature-requirement} ?ò¥µ¥Ý¡??Ȥ??Ƥ????????Î¥Þ¥??? 65: @var{command-or-definition} @dots{} ??????Þ¤??? 66: ?Û¤?heme ?μ?????ǽ?κ??Û¤??Τ˻Ȥ???????????礦?? 67: 68: @var{feature-requiremnt} ?ϰʲ??Τ褦?ʹ?ʸ?Ǥʤ????ʤ?????@c COMMON 69: @example 70: @var{feature-requirement} 71: : @var{feature-identifier} 72: | (and @var{feature-requirement} @dots{}) 73: | (or @var{feature-requirement} @dots{}) 74: | (not @var{feature-requirement}) 75: @end example 76: 77: @c EN 78: @var{feature-identifier} is a symbol that indicates a feature. 79: If such a feature is supported in Gauche, it satisfies the 80: @var{feature-requirement}. You can do boolean combination 81: of @var{feature-requirement}s to compose more complex conditions. 82: 83: The macro tests each @var{feature-requirement} in order, and 84: if one is satisfied, the macro itself expands to the 85: corresponding @var{command-or-definition} @dots{}. 86: 87: The last clause may begin with a symbol @code{else} instead of 88: @var{feature-requirement}. If no condition is fulfilled before 89: it, then the macro expands to the @var{command-or-definition}s 90: of the else clause. If there's no else clause and no condition 91: is fulfilled, an error is signalled. 92: 93: Currently, Gauche recognizes srfi names (e.g. @code{srfi-1}) 94: that it supports (@xref{Standard conformance}, for the list of 95: supported SRFIs), and a symbol @code{gauche} as @var{feature-identifier}. 96: 97: Suppose you are writing a program that requires srfi-13 (string library). 98: In Gauche, the standard way to use srfi-13 is like this: 99: @c JP 100: @var{feature-identifier} ?ϵ?ǽ?ò¼¨¤?????????????뵡ǽ?? Gauche ?????Ý¡??Ȥ????????é¡¢??????@var{feature-requirement} ??????Þ¤??? 101: ??Ê£???ʾ??????뤿????@var{feature-requirement} ?Î¥Ö¡?????? 102: ??ß¹???????뤳?Ȥ???????? 103: 104: ???Î¥Þ¥??? @var{feature-requirement} ??˥ƥ??Ȥ??????ΤҤȤĤ? 105: ??????줿?é¡¢???????var{command-or-definition} @dots{} ????????????? 106: 107: ?Ǹ???Ï¡?@var{feature-requirement} ?ǤϤʤ???@code{else} ?ǻϤ????Ȥ?Ǥ??Þ¤????????????????줿????ʤ????ˤϡ????Î¥Þ¥??? else ???@var{command-or-definition} ??????????????⤷??else ????ʤ??? 108: ?????????᤬?ҤȤĤ??????????顼??ȯ????Þ¤??? 109: 110: ???????Gauche ?Ǥϡ????Ý¡??Ȥ???????RFI ?ε?ǽ?Ï¡?srfi ̾ 111: (???Ȥ??С?@code{srfi-1})?Ǽ????????(???Ý¡??Ȥ???????RFI ???Ĥ??Ƥ?@ref{Standard conformance} ?ò»²¾È¤??Ƥ??????)?? 112: ?Þ¤????????code{gauche} ??var{feature-identifier} ?Ȥ?????????????? 113: 114: srfi-13 (ʸ?????Ö¥?)??פȤ????í¥°??????Ƥ??????Þ¤??礦?? 115: Gauche ?Ǥϡ?ɸ???ˤϡ??ʲ??Τ褦?ˤ??Þ¤??? 116: @c COMMON 117: 118: @example 119: (use srfi-13) 120: 121: @c EN 122: ;; ... your program ... 123: @c JP 124: ;; ... ???ʤ??Î¥×¥í¥°?? ... 125: @c COMMON 126: @end example 127: 128: @c EN 129: However, the 'use' macro is Gauche specific and won't work 130: on other Scheme implementations. You can write instead: 131: @c JP 132: ????????????'use' ?Þ¥??? Gauche ???Ç¡?¾??Scheme ?μ???ư??Þ¤?????˰ʲ??Τ褦?˽ñ¤¯¤??Ȥ??Ǥ??Þ¤??? 133: @c COMMON 134: 135: @example 136: (cond-expand 137: (srfi-13 138: @c EN 139: ;; ... your program ... 140: @c JP 141: ;; ... ???ʤ??Î¥×¥í¥°?? ... 142: @c COMMON 143: )) 144: @end example 145: 146: @c EN 147: If the implementation supports @code{srfi-13}, the form expands 148: to your program. Note that Gauche issues @code{(use srfi-13)} 149: for you in this case. 150: 151: The above form signals an error if the implementation doesn't support 152: srfi-13. However, you might want to run your program on such implementations 153: as well, by providing alternative library definitions. 154: @c JP 155: ???μ??@code{srfi-13} ?ò¥µ¥Ý¡??Ȥ??Ƥ????????η???ϡ֤??ʤ????×¥í¥°???פ????????????Gauche ?Ǥϡ????ξ???????code{(use srfi-13)}??ӽФ??Þ¤??? 156: 157: ?åµ?Î¥Õ¥????????rfi-13?ò¥µ¥Ý¡??Ȥ??Ƥ??ʤ??????顼?Ȥʤ????? 158: ???Τ褦?ʼ??????í¥°????餻???????Ï¡? 159: ???Ȥʤ????Ö¥??????????????Ǥ??礦?? 160: @c COMMON 161: 162: @example 163: (cond-expand 164: (srfi-13 165: @c EN 166: ;; ... your program ... 167: @c JP 168: ;; ... ???ʤ??Î¥×¥í¥°?? ... 169: @c COMMON 170: ) 171: (else 172: (load "my-srfi-13-implementation") 173: @c EN 174: ;; ... your program ... 175: @c JP 176: ;; ... ???ʤ??Î¥×¥í¥°?? ... 177: @c COMMON 178: )) 179: @end example 180: @end defmac 181: 182: @c ---------------------------------------------------------------------- 183: @node List library, Homogeneous vectors, Feature conditional, Library modules - SRFIs 184: @section @code{srfi-1} - List library 185: 186: @deftp {Module} srfi-1 187: @mdindex srfi-1 188: @c EN 189: SRFI-1 is a rich collection of list manipulation library 190: (@ref{srfi-1,[SRFI-1],SRFI-1}). 191: It is available by saying @code{(use srfi-1)}. 192: The implementation is based on Olin Shivers's reference implementation. 193: @c JP 194: SRFI-1 ?Ï¡??ꥹ????饤?Ö¥??????Ê¥??쥯?????Ǥ? 195: (@ref{srfi-1,[SRFI-1],SRFI-1})?? 196: ???Υ饤?Ö¥?????ˤϡ?@code{(use srfi-1)} ?Ȥ??Ʋ??????? 197: Olin Shivers????????????????Ƽ?õ¤µ¤????Þ¤??? 198: @c COMMON 199: @end deftp 200: 201: @menu 202: * SRFI-1 List constructors:: 203: * SRFI-1 List predicates:: 204: * SRFI-1 List selectors:: 205: * SRFI-1 List miscellaneous routines:: 206: * SRFI-1 Fold:: 207: * SRFI-1 Filtering & Partitioning:: 208: * SRFI-1 Searching:: 209: * SRFI-1 Deletion:: 210: * SRFI-1 Association lists:: 211: * SRFI-1 Set operations:: 212: @end menu 213: 214: @node SRFI-1 List constructors, SRFI-1 List predicates, List library, List library 215: @subsection List constructors 216: @c NODE SRFI-1 ?ꥹ?ȹ???? ?ꥹ?Ȥι???? 217: @defun xcons cd ca 218: @c EN 219: [SRFI-1] Equivalent to @code{(cons ca cd)}. Useful to pass to 220: higher-order procedures. 221: @c JP 222: [SRFI-1] @code{(cons ca cd)} ?????Ǥ????ⳬ??????Ϥ??Τ????? 223: @c COMMON 224: @end defun 225: 226: @defun cons* elt1 elt2 @dots{} 227: @c EN 228: [SRFI-1] Like @code{list}, but the last argument provides the tail of 229: the constructed list. This is just a synonym of Gauche built-in procedure 230: @code{list*}. 231: @c JP 232: [SRFI-1] @code{list} ?È»???Þ¤??????Ǹ?????????줿?ꥹ?Ȥ????ˤʤ?????Gauche ??Ȥ߹??ß¼??? @code{list*} ????դǤ??? 233: @c COMMON 234: @example 235: (cons* 1 2 3 4) @result{} (1 2 3 . 4) 236: (cons* 1) @result{} 1 237: @end example 238: @end defun 239: 240: @defun list-tabulate n init-proc 241: @c EN 242: [SRFI-1] Constructs an @var{n}-element list, in which 243: each element is generated by @code{(@var{init-proc} @i{i})}. 244: @c JP 245: [SRFI-1] @var{n}?Ĥ??Ǥ?ĥꥹ?Ȥ?Û¤??????줾??????ode{(@var{init-proc} @i{i})} ???????Þ¤??? 246: @c COMMON 247: @example 248: (list-tabulate 4 values) @result{} (0 1 2 3) 249: @end example 250: @end defun 251: 252: @defun circular-list elt1 elt2 @dots{} 253: @c EN 254: [SRFI-1] Constructs a circular list of the elements. 255: @c JP 256: [SRFI-1] ??ꤷ??????Ľ۴ĥꥹ?Ȥ?Û¤??Þ¤??? 257: @c COMMON 258: @example 259: (circular-list 'z 'q) @result{} (z q z q z q @dots{}) 260: @end example 261: @end defun 262: 263: @defun iota count &optional (start 0) (step 1) 264: @c EN 265: [SRFI-1] Returns a list of numbers, starting from @var{start}, 266: increasing by @var{step}. 267: @c JP 268: [SRFI-1] @var{start}?????ޤꡢ@var{step}?????ë¡¢ 269: @var{count} ?Ĥ??Ǥ???????Υꥹ?Ȥ???Þ¤??? 270: @c COMMON 271: @example 272: (iota 5) @result{} (0 1 2 3 4) 273: (iota 5 0 -0.1) @result{} (0 -0.1 -0.2 -0.3 -0.4) 274: @end example 275: @end defun 276: 277: @node SRFI-1 List predicates, SRFI-1 List selectors, SRFI-1 List constructors, List library 278: @subsection List predicates 279: @c NODE SRFI-1 ?ꥹ?ȤνҸ??ꥹ?ȤνҸ?@defun proper-list? x 280: @c EN 281: [SRFI-1] Returns @code{#t} if x is a proper list. 282: @c JP 283: [SRFI-1] @var{x} ??????ꥹ?ȤǤ??? @code{#t} ????Þ¤??? 284: @c COMMON 285: @end defun 286: 287: @defun circular-list? x 288: @c EN 289: [SRFI-1] Returns @code{#t} if x is a circular list. 290: @c JP 291: [SRFI-1] @var{x} ???۴ĥꥹ?ȤǤ??? @code{#t} ????Þ¤??? 292: @c COMMON 293: @end defun 294: 295: @defun dotted-list? x 296: @c EN 297: [SRFI-1] Returns @code{#t} if x is a finite, non-nil-terminated list. 298: This includes non-pair, non-() values (e.g. symbols, numbers), 299: which are considered to be dotted lists of length 0. 300: @c JP 301: [SRFI-1] @var{x} ??Í?¤?ç¤???Ç¡?????Ȥǽ?ü???Ƥ??ʤ??ꥹ?Ȥʤ?code{#t} ????Þ¤????????Ï¡??Ú¥??ǤϤʤ???????Ȥ????(???Ȥ???????????Τ褦?????0?Υɥåȥꥹ?Ȥȹͤ??????Τ?ߤޤ??? 302: @c COMMON 303: @end defun 304: 305: @defun null-list? list 306: @c EN 307: [SRFI-1] Returns @code{#t} if @var{list} is the empty list @code{()}, 308: and @var{#f} otherwise. 309: @c JP 310: [SRFI-1] @var{list} ???????@code{()} ?ʤ?code{#t} ????Þ¤??? 311: ???????ΤȤ???@var{#f} ????Þ¤??? 312: @c COMMON 313: @end defun 314: 315: @defun not-pair? x 316: @c EN 317: [SRFI-1] @code{(lambda (x) (not (pair? x)))}. 318: 319: SRFI-1 says: Provided as a procedure as it can be useful as 320: the termination condition for list-processing procedures that wish to 321: handle all finite lists, both proper and dotted. 322: @c JP 323: [SRFI-1] @code{(lambda (x) (not (pair? x)))}??????Ǥ??? 324: 325: SRFI-1 ?Ǥϡ??Ö¿???ꥹ?Ȥ????ɥåȥꥹ?Ȥ????Ç¡????٤Ƥ???¥ꥹ?Ȥ?????????ν?ü??Ȥ?????????Ѱդ????פȤ??????? 326: @c COMMON 327: @end defun 328: 329: @defun list= elt= list @dots{} 330: @c EN 331: [SRFI-1] Determines list equality by comparing every n-th element 332: of given lists by the procedure @var{elt=}. 333: 334: It is an error to apply @code{list=} to anything except proper lists. 335: 336: The equality procedure must be consistent with @code{eq?}, i.e. 337: @c JP 338: [SRFI-1] @var{elt=} ????Æ¡?n?????Ǥò¤½¤ì¤¾???Ó¤??뤳?Ȥǡ? 339: Í¿???????ꥹ?Ȥ??????ꤷ?Þ¤??? 340: 341: @code{list=} ????ꥹ?Ȱʳ????????????顼?ˤʤ????? 342: 343: Ʊ??Ƚ??μ?????@code{eq?} ????????ʤ????ʤ????󡣤??ʤ? 344: @c COMMON 345: @example 346: (eq? x y) @result{} (elt= x y). 347: @end example 348: @end defun 349: 350: @node SRFI-1 List selectors, SRFI-1 List miscellaneous routines, SRFI-1 List predicates, List library 351: @subsection List selectors 352: @c NODE SRFI-1 ?ꥹ?????? ?ꥹ?????? 353: @defun first pair 354: @defunx second pair 355: @defunx third pair 356: @defunx fourth pair 357: @defunx fifth pair 358: @defunx sixth pair 359: @defunx seventh pair 360: @defunx eighth pair 361: @defunx ninth pair 362: @defunx tenth pair 363: @c EN 364: [SRFI-1] Returns n-th element of the (maybe improper) list. 365: @c JP 366: [SRFI-1] ?ꥹ???????Ǥ?)???????Ǥ???Þ¤??? 367: @c COMMON 368: @end defun 369: 370: @defun car+cdr pair 371: @c EN 372: [SRFI-1] Returns two values, @code{(car pair)} and @code{(cdr pair)}. 373: @c JP 374: [SRFI-1] @code{(car pair)} ???? @code{(cdr pair)} ????ͤ???Þ¤??? 375: @c COMMON 376: @end defun 377: 378: @defun take x i 379: @defunx drop x i 380: @c EN 381: [SRFI-1] @code{take} returns the first i elements of list x. 382: @code{drop} returns all but the first i elements of list x. 383: @c JP 384: [SRFI-1] 385: @code{take} ?ϥꥹ??@var{x} ?κǽ?i?Ĥ??Ǥ???Þ¤??? 386: @code{drop} ?ϥꥹ??@var{x} ?κǽ?i?Ĥ??Ǥ?????ꥹ?Ȥ???Þ¤??? 387: @c COMMON 388: @example 389: (take '(a b c d e) 2) => (a b) 390: (drop '(a b c d e) 2) => (c d e) 391: @end example 392: @c EN 393: @var{x} may be any value: 394: @c JP 395: @var{x} ?Ϥ???????ꤨ?Þ¤??? 396: @c COMMON 397: @example 398: (take '(1 2 3 . d) 2) => (1 2) 399: (drop '(1 2 3 . d) 2) => (3 . d) 400: (drop '(1 2 3 . d) 3) => d 401: @end example 402: @c EN 403: @code{drop} is exactly equivalent to performing @var{i} cdr operations 404: on @var{x}. The returned value shares a common tail with @var{x}. 405: On the other hand, take always allocates a new list 406: for result if the argument is a list of non-zero length. 407: 408: An error is signalled if @var{i} is past the end of list @var{x}. 409: @xref{Additional list library}, for more tolerant version 410: of @code{take} and @code{drop}. 411: 412: For generic subsequence extraction from any sequence, see @code{subseq} 413: in @ref{Slicing sequence}. 414: @c JP 415: @code{drop} ??@var{x} ??@var{i} ??r ????????ʤ??Τ???? 416: Ʊ???Ǥ??????????Ï¡?@var{x} ?ȶ??????????Þ¤??????? 417: take ?Ï¡????ꥹ?Ȥ?Ĺ??0?Ǥʤ??ꥹ?Ȥʤ??????????ꥹ?Ȥ??????ݤ??Þ¤??? 418: 419: @var{i} ???ꥹ??@var{x} ?ν?ü??????饨?顼??ȯ????Þ¤??? 420: ??????ʥС???????@code{take} ??@code{drop} ?ˤĤ??Ƥϡ? 421: @xref{Additional list library} ?ò»²¾È¤??Ƥ???????? 422: 423: ???????Ó¤????ʬ??Ó¤?Ф????Ū???ؤ??Ƥϡ? 424: @ref{Slicing sequence} ?ˤ???code{subseq} ?ò»²¾È¤??Ƥ???????? 425: @c COMMON 426: @end defun 427: 428: @defun take-right flist i 429: @defunx drop-right flist i 430: @c EN 431: [SRFI-1] @code{take-right} returns the last @var{i} elements of @var{flist}. 432: @code{drop-right} returns all but the last @var{i} elements of @var{flist}. 433: @c JP 434: [SRFI-1] @code{take-right} ??@var{flist} ?κǸ? @var{i}?Ĥ????????????Ȥ???Þ¤??? 435: @code{drop-right} ??@var{flist} ?κǸ? @var{i}?Ĥ??Ǥ??????ꥹ??Ö¤??Þ¤??? 436: @c COMMON 437: @example 438: (take-right '(a b c d e) 2) => (d e) 439: (drop-right '(a b c d e) 2) => (a b c) 440: @end example 441: @c EN 442: @var{flist} may be any finite list. 443: @c JP 444: @var{flist} ????¥ꥹ?ȤǤ???OK?Ǥ??? 445: @c COMMON 446: @example 447: (take-right '(1 2 3 . d) 2) => (2 3 . d) 448: (drop-right '(1 2 3 . d) 2) => (1) 449: (take-right '(1 2 3 . d) 0) => d 450: (drop-right '(1 2 3 . d) 0) => (1 2 3) 451: @end example 452: @c EN 453: @code{take-right}'s return value always shares a common 454: tail with @var{flist}. 455: @code{drop-right} always allocates a new list 456: if the argument is a list of non-zero length. 457: 458: An error is signalled if @var{i} is larger than the length of @var{flist}. 459: @xref{Additional list library}, for more tolerant version 460: of @code{take-right} and @code{drop-right}. 461: @c JP 462: @code{take-right} ??Ö¤???Ϥ??ĤǤ?var{first} ?ζ??????????Þ¤??? 463: @code{drop-right} ?Ï¡???Ĺ????0?Ǥʤ??ꥹ?Ȥʤ顢ɬ?????????ꥹ?Ȥ??????ݤ??Þ¤??? 464: 465: @var{i} ???ꥹ??@var{flist} ?????????????????顼??ȯ????Þ¤??? 466: ??????ʥС???????@code{take-right} ??@code{drop-right} ?ˤĤ??Ƥϡ? 467: @xref{Additional list library} ?ò»²¾È¤??Ƥ???????? 468: @c COMMON 469: @end defun 470: 471: @defun take! x i 472: @defunx drop-right! x i 473: @c EN 474: [SRFI-1] Linear update variants of @var{take} and @var{drop-right}. 475: Those procedures may destructively modifies @var{x}. 476: 477: If @var{x} is circular, @code{take!} may return a list 478: shorter than expected. 479: @c JP 480: [SRFI-1] @var{take} ???? @var{drop-right} ?????ξ??????????С??????Ǥ???????????????@var{x} ????ѹ????뤫?⤷?????? 481: @var{x} ???۴ĥꥹ?Ȥʤ顢@code{take!} ?Ï´??????û???ꥹ?Ȥ?? 482: ?????????????? 483: @c COMMON 484: @end defun 485: 486: @defun split-at x i 487: @defunx split-at! x i 488: @c EN 489: [SRFI-1] @code{split-at} splits the list @var{x} at index @var{i}, 490: returning a list of the first @var{i} elements, and the remaining tail. 491: @c JP 492: [SRFI-1] @code{split-at} ?ϥꥹ??@var{x} ?ò¥¤¥?Ã¥??? @var{i} ????Ö¤???䤷???ǽ? @var{i} ?Ĥ??Ǥ????????Ȥȡ??Ĥ????Ȥ????Þ¤??? 493: @c COMMON 494: @example 495: (split-at '(a b c d e) 2) @result{} (a b) (c d e) 496: @end example 497: @c EN 498: @code{split-at!} is the linear-update variant. It may destructively 499: modifies @var{x} to produce the result. 500: @c JP 501: @code{split-at!} ?Ϥ??ξ??????????С??????Ǥ??? 502: ???? @var{x} ???˹??????뤫?⤷??????@c COMMON 503: @end defun 504: 505: @defun last pair 506: @c EN 507: [SRFI-1] Returns the last element of the non-empty, finite list @var{pair}. 508: It is equivalent to @code{(car (last-pair pair))}. 509: Note that @code{last-pair} is Gauche built-in procedure. 510: @c JP 511: [SRFI-1] ??Ϥʤ?Í?¥ꥹ??@var{pair} ?κǸ???????Þ¤??? 512: ??????@code{(car (last-pair pair))} ?????Ǥ??? 513: 514: ?: @code{last-pair} ??Gauche ??Ȥ߹??ß¼????Ǥ??? 515: @c COMMON 516: @end defun 517: 518: @node SRFI-1 List miscellaneous routines, SRFI-1 Fold, SRFI-1 List selectors, List library 519: @subsection List miscellaneous routines 520: @c NODE ??Υꥹ?Ƚ??롼???defun length+ x 521: @ EN 522: [SRFI-1] If @var{x} is a proper list, returns its length. 523: Otherwise, returns @code{#f}. 524: @c JP 525: [SRFI-1] @var{x} ??????ꥹ?Ȥʤ顢???????????Þ¤????????????? 526: @code{#f} ????Þ¤??? 527: @c COMMON 528: @end defun 529: 530: @defun concatenate list-of-lists 531: @defunx concatenate! list-of-lists! 532: @c EN 533: [SRFI-1] Equivalent to @code{(apply append @var{list-of-lists})} and 534: @code{(apply append! @var{list-of-lists})}, respectively. 535: @c JP 536: [SRFI-1] ???줾?ì¡¢@code{(apply append @var{list-of-lists})} ???? 537: @code{(apply append! @var{list-of-lists})} ?????Ǥ??? 538: @c COMMON 539: @end defun 540: 541: @defun append-reverse rev-head tail 542: @defunx append-reverse! rev-head tail 543: @c EN 544: [SRFI-1] @code{append-reverse} returns @code{(append (reverse rev-head) tail)}. 545: @code{append-reverse!} is the linear-update variant. 546: @c JP 547: [SRFI-1] @code{append-reverse} ??@code{(append (reverse rev-head) tail)} ?????Þ¤???@code{append-reverse!} ?Ϥ??ξ??????????С??????Ǥ??? 548: @c COMMON 549: @end defun 550: 551: @defun zip clist1 clist2 @dots{} 552: @c EN 553: [SRFI-1] Equivalent to @code{(map list clist1 clist2 @dots{})}. 554: If @code{zip} is passed @var{n} lists, it returns a list as long as 555: the shortest of these lists, each element of which is an @var{n}-element list 556: comprised of the corresponding elements from the parameter lists. 557: @c JP 558: [SRFI-1] @code{(map list clist1 clist2 @dots{})} ?????Ǥ??? 559: @var{n} ??Υꥹ?Ȥ? @code{zip} ??Ϥ??줿???ˤϡ????Τʤ??ǰ?û??????Ʊ??Ĺ???Υꥹ?Ȥ???Þ¤???????줿?ꥹ?Ȥϡ????? @var{n} ???Υꥹ?Ȥǡ? 560: ???Τ??줾?줬????????Ϥäƥꥹ?Ȥ?б???????ˤʤäƤ??Þ¤??? 561: @c COMMON 562: @example 563: (zip '(one two three) 564: '(1 2 3) 565: '(odd even odd even odd even odd even)) 566: @result{} ((one 1 odd) (two 2 even) (three 3 odd)) 567: 568: (zip '(1 2 3)) @result{} ((1) (2) (3)) 569: @end example 570: @c EN 571: At least one of the argument lists must be finite: 572: @c JP 573: ???ꥹ?ȤΤ?????????Ȥ??ȤĤ???¤Υꥹ?ȤǤʤ????ʤ?????@c COMMON 574: @example 575: (zip '(3 1 4 1) (circular-list #f #t)) 576: @result{} ((3 #f) (1 #t) (4 #f) (1 #t)) 577: @end example 578: @end defun 579: 580: @defun unzip1 list 581: @defunx unzip2 list 582: @defunx unzip3 list 583: @defunx unzip4 list 584: @defunx unzip5 list 585: @c EN 586: [SRFI-1] @code{unzip1} takes a list of lists, where every list must 587: contain at least one element, and returns a list containing the 588: initial element of each such list. 589: @code{unzip2} takes a list of lists, where every list 590: must contain at least two elements, and returns two values: 591: a list of the first elements, and a list of the second 592: elements. @code{unzip3} does the same for the first 593: three elements of the lists, and so on. 594: @c JP 595: [SRFI-1] @code{unzip1} ?ϥꥹ?ȤΥꥹ?Ȥ????ƤȤ????????줾?? 596: ?ꥹ?ȤϾ????Ȥ??Ĥ??Ǥ???ΤǤʤ??ƤϤʤ????ó¡£·??Ȥ??????줾???ꥹ?Ȥκǽ????Υꥹ?Ȥ???Þ¤??? 597: @code{unzip2} ?ϥꥹ?ȤΥꥹ?Ȥ????ƤȤ????????줾???ꥹ?Ȥ??????Ȥ??Ĥ??Ǥ???ΤǤʤ??ƤϤʤ????ó¡£·??Ȥ?????ͤ????Þ¤????ǽ????Υꥹ?Ȥ??ܤ??ǤΥꥹ?ȤǤ???@code{unzip3} ??3???ޤǤ??ǤˤĤ??????Ǥ????ʲ?????Ǥ??? 598: @c COMMON 599: @example 600: (unzip2 '((1 one) (2 two) (3 three))) @result{} 601: (1 2 3) @r{and} 602: (one two three) 603: @end example 604: @end defun 605: 606: @defun count pred clist1 clist2 @dots{} 607: @c EN 608: [SRFI-1] A procedure @var{pred} is applied to the @var{n}-th element 609: of given lists, from @var{n} is zero to the length of the 610: the shortest finite list in the given lists, 611: and the count of times @var{pred} returned true is returned. 612: @c JP 613: [SRFI-1] @var{n} ?ò¥¼¥í¤«?????????ꥹ?ȤΤ???Ǥ????ꥹ?Ȥ?Ĺ???ޤǤȤ??Æ¡?@var{pred} ????????????ꥹ?Ȥ?@var{n} ?????Ǥ????줾??????Þ¤??? 614: @var{pred} ???????????Ö¤????? 615: @c COMMON 616: @example 617: (count even? '(3 1 4 1 5 9 2 5 6)) @result{} 3 618: (count < '(1 2 4 8) '(2 4 6 8 10 12 14 16)) @result{} 3 619: @end example 620: @c EN 621: At least one of the argument lists must be finite: 622: @c JP 623: ??Í¿?????????Ȥξ????Ȥ??ȤĤ???¤Ǥʤ????ʤ?????@c COMMON 624: @example 625: (count < '(3 1 4 1) (circular-list 1 10)) @result{} 2 626: @end example 627: @end defun 628: 629: @node SRFI-1 Fold, SRFI-1 Filtering & Partitioning, SRFI-1 List miscellaneous routines, List library 630: @subsection List fold, unfold & map 631: @c NODE ?ꥹ?Ȥξ????fold)???ò¤¤Û¤???(unfold)????????? 632: 633: @defun fold kons knil clist1 clist2 @dots{} 634: @c EN 635: [SRFI-1] The fundamental list iterator. 636: When it is given a single list 637: @var{clist1} = (@var{e1} @var{e2} @dots{} @var{en}), 638: then this procedure returns 639: @c JP 640: [SRFI-1] ??ꥹ?????é»»?ҤǤ???ñ???ꥹ??@var{clist1} = (@var{e1} @var{e2} @dots{} @var{en}) 641: ????????Ȥ??ˤϡ??ʲ?????Þ¤??? 642: @c COMMON 643: @example 644: (@var{kons} @var{en} @dots{} (@var{kons} @var{e2} (@var{kons} @var{e1} @var{knil})) @dots{} ) 645: @end example 646: 647: @c EN 648: If @var{n} list arguments are provided, then the @var{kons} 649: function must take @var{n}+1 parameters: one element from each list, and 650: the "seed" or fold state, which is initially @var{knil}. 651: The fold operation terminates when the shortest list runs out of values. 652: At least one of the list arguments must be finite. 653: 654: Examples: 655: @c JP 656: @var{n} ??Υꥹ?Ȥ?Í¿?????????ˤϡ?@var{kons} ?Ø¿?@var{n}+1 ?Ĥΰ?????ʤ????ʤ????󡣤??줾???ꥹ?Ȥ??????ȤĤ??Ĥȡ? 657: ??? @var{knil} ?Ǥ????????뤤?Ͼ???ß¾?Ȥ??????Ǥ??? 658: ???ξ???߱黻?Ï¡????Ȥ????ꥹ?Ȥ??Ǥ??ʤ??ʤä??Ȥ?????λ???Þ¤??? 659: Í¿?????????Ȥξ????Ȥ??ȤĤ???¤Ǥʤ????ʤ????? 660: ?: 661: @c COMMON 662: @example 663: (fold + 0 '(3 1 4 1 5 9)) @result{} 23 ;@r{sum up the elements} 664: (fold cons '() '(a b c d e)) @result{} (e d c b a) ;@r{reverse} 665: (fold cons* '() '(a b c) '(1 2 3 4 5)) 666: @result{} (c 3 b 2 a 1) ;@r{n-ary case} 667: @end example 668: @end defun 669: 670: @defun fold-right kons knil clist1 clist2 @dots{} 671: @c EN 672: [SRFI-1] The fundamental list recursion operator. 673: When it is given a single list 674: @var{clist1} = (@var{e1} @var{e2} @dots{} @var{en}), 675: then this procedure returns 676: @c JP 677: [SRFI-1] ??ꥹ?ȺƵ??é»»?ҤǤ???ñ???ꥹ??@var{clist1} = (@var{e1} @var{e2} @dots{} @var{en}) 678: ????????Ȥ??ˤϡ??ʲ?????Þ¤??? 679: @c COMMON 680: @example 681: (@var{kons} @var{e1} (@var{kons} @var{e2} @dots{} (@var{kons} @var{en} @var{knil}))) 682: @end example 683: 684: @c EN 685: If @var{n} list arguments are provided, then the @var{kons} function 686: must take @var{n}+1 parameters: one element from each list, and 687: the "seed" or fold state, which is initially @var{knil}. 688: The fold operation terminates when the shortest list runs out of values. 689: At least one of the list arguments must be finite. 690: 691: Examples: 692: @c JP 693: @var{n} ??Υꥹ?Ȥ?Í¿?????????ˤϡ?@var{kons} ?Ø¿?@var{n}+1 ?Ĥΰ?????ʤ????ʤ????󡣤??줾???ꥹ?Ȥ??????ȤĤ??Ĥȡ? 694: ??? @var{knil} ?Ǥ????????뤤?Ͼ???ß¾?Ȥ????ΤǤ??? 695: ???ξ???߱黻?Ï¡????Ȥ????ꥹ?Ȥ??Ǥ??ʤ??ʤä??Ȥ?????λ???Þ¤??? 696: Í¿?????????Ȥξ????Ȥ??ȤĤ???¤Ǥʤ????ʤ????? 697: ?: 698: @c COMMON 699: @example 700: (fold-right cons '() '(a b c d e)) 701: @result{} (a b c d e) ;@r{copy list} 702: (fold-right cons* '() '(a b c) '(1 2 3 4 5)) 703: @result{} (a 1 b 2 c 3) ;@r{n-ary case} 704: @end example 705: @end defun 706: 707: @defun pair-fold kons knil clist1 clist2 @dots{} 708: @defunx pair-fold-right kons knil clist1 clist2 @dots{} 709: @c EN 710: [SRFI-1] 711: Like @code{fold} and @code{fold-right}, but the procedure @var{kons} 712: gets each @code{cdr} of the given @var{clist}s, instead of @code{car}. 713: @c JP 714: [SRFI-1] 715: @code{fold} ???? @code{fold-right} ?????Ǥ?????@var{kons} ???? 716: ????????? @var{clist} ??@code{car} ?ǤϤʤ???@code{cdr} ??????? 717: @c COMMON 718: @end defun 719: 720: @defun reduce f ridentity list 721: @defunx reduce-right f ridentity list 722: @c EN 723: [SRFI-1] 724: Variant of @code{fold} and @code{fold-right}. @var{f} must be a 725: binary operator, and @var{ridentity} is the value such that for 726: any value @var{x} that is valid as @var{f}'s input, 727: @c JP 728: [SRFI-1] 729: @code{fold} ???? @code{fold-right} ??Ñ·?С??????Ǥ??? 730: @var{f} ???é»»?ҤǤʤ????ʤ??????Þ¤???@var{ridentity} ??@var{f} ?????Ȥ??Ƶö¤µ¤? 731: ?????? @var{x} ?ˤĤ??ưʲ?????Ƥ??ʤ????ʤ?????@c COMMON 732: @example 733: (f x ridentity) @equiv{} x 734: @end example 735: 736: @c EN 737: These functions effectively do the same thing as @code{fold} or 738: @code{fold-right}, respectively, but omit the first 739: application of @var{f} to @var{ridentity}, using the above nature. 740: So @var{ridentity} is used only when @var{list} is empty. 741: @c JP 742: ?????δؿ?¼????@code{fold} ??code{fold-right} ????????Ȥ????Þ¤?????@var{ridentity}?ˤϾåµ?????????뤿?á¡¢ 743: @var{f}??var{ridentity}?ˤ???????????@var{ridentity}???Ȥ?????var{list}?????????Ǥ??? 744: @c COMMON 745: @end defun 746: 747: @defun unfold p f g seed &optional tail-gen 748: @c EN 749: [SRFI-1] 750: Fundamental recursive list constructor. 751: Defined by the following recursion. 752: @c JP 753: [SRFI-1] 754: ??ꥹ?ȺƵ?????ҤǤ??? 755: ?ʲ??Τ褦?˺Ƶ?Ū??????????Þ¤??? 756: @c COMMON 757: 758: @example 759: (unfold p f g seed tail-gen) @equiv{} 760: (if (p seed) 761: (tail-gen seed) 762: (cons (f seed) 763: (unfold p f g (g seed)))) 764: @end example 765: @c EN 766: That is, @var{p} determines where to stop, @var{g} is used to generate 767: successive seed value from the current seed value, and @var{f} is used 768: to map each seed value to a list element. 769: @c JP 770: ?????Ǥϡ?@var{p} ?Ͻ?λ??Ö¤?????@var{g} ?ϸ??ߤΡּ????鼡?Ρּ? 771: ?????????Ѥ???@var{f} ?Ϥ??줾???Ö¼?????Ȥ??Ǥ?Ñ´?????Τ?Ѥ????Þ¤??? 772: @c COMMON 773: @end defun 774: 775: <