
1: @node Core syntax, Macros, Programming in Gauche, Top 2: @chapter Core syntax 3: @c NODE ????ʹ?ʸ 4: 5: @c ---------------------------------------------------------------------- 6: @menu 7: * Lexical structure:: 8: * Literals:: 9: * Making Procedures:: 10: * Assignments:: 11: * Conditionals:: 12: * Binding constructs:: 13: * Sequencing:: 14: * Iteration:: 15: * Quasiquotation:: 16: * Definitions:: 17: * Modules:: 18: @end menu 19: 20: @node Lexical structure, Literals, Core syntax, Core syntax 21: @section Lexical structure 22: @c NODE ??¤ 23: 24: @c EN 25: Gauche extends R5RS Scheme parser in some ways: 26: @c JP 27: Gauche??5RS Scheme?Υѡ????ò¤¤¤??Ĥ????dz?????Ƥ??Þ¤??? 28: @c COMMON 29: 30: @table @emph 31: @c EN 32: @item Nested block comments (SRFI-30) 33: A text surrounded by '@code{#|}' and '@code{|#}' becomes a comment. 34: It can be nested. 35: @c JP 36: @item ?Í¥??Ȳ???Ê¥Ö¥?????????(SRFI-30) 37: '@code{#|}' ??'@code{|#}' ?ǰϤޤ줿?Æ¥????Ȥϥ????Ȥˤʤ????? 38: ?Í¥??Ȳ???Ǥ??? 39: @c COMMON 40: 41: @c EN 42: @item Numeric literals 43: Either integral part or fraction part of inexact real numbers can be omitted 44: if it is zero, i.e. @code{30.}, @code{.25}, @code{-.4} are read as 45: real numbers. 46: The number reader recognizes '@code{#}' as insignificant digits. 47: Complex numbers can be written both in the rectangular format 48: (e.g. @code{1+0.3i}) and in the polar format (e.g. 3.0@@1.57). 49: Inexact real numbers include 50: the positive infinity, the negative infinity, and NaN, 51: which are represented as @code{+inf.0}, @code{-inf.0} and @code{+nan.0}, 52: respectively. (@code{-nan.0} is also read as NaN.) 53: @c JP 54: @item ????? 55: ???Τʼ¿????Þ¤??Ͼ???Τ????줫??0?ξ?????????????뤳?Ȥ? 56: ?Ǥ??Þ¤??????@code{30.}, @code{.25}, @code{-.4}?? 57: ??Υ꡼??Ï¡??ѥǥ??ó¥°¤?@code{#}'??????Þ¤??? 58: Ê£????????????(???@code{1+0.3i}) ?Ǥ??????(???3.0@@1.57) ?Ǥ???????????? 59: ???Τʼ¿????????ç¡¢??????ç¡¢???N)??ß¡? 60: ?????Ϥ??줾??ode{+inf.0}??@code{-inf.0}??????@code{+nan.0}??ɽ??????????(@code{-nan.0}????N)?Ȥ???ɤޤ???)?? 61: @c COMMON 62: 63: @c EN 64: @item Symbol names 65: Symbol names are case sensitive by default (@xref{Case-sensitivity}). 66: Symbol name can begin with digits, '@code{+}' or '@code{-}', as long as 67: the entire token doesn't consist valid number syntax. 68: Other weird characters can be included in a symbol name by surrounding 69: it with '|', e.g. '@code{|this is a symbol|}'. 70: @xref{Symbols}, for details. 71: @c JP 72: @item ????? 73: ??????ϥǥե??????????????Þ¤? (@ref{Case-sensitivity}?????? 74: ???code{+}', '@code{-}' ?ǻϤޤ륷????â¡¢ 75: ?È¡???????????????????ʤ??¤????????? 76: ?Þ¤???¾??Ѥ????|' ?Ç¥?????Ϥळ?Ȥˤ??Æ¥?????Ë´Þ¤????Ȥ? 77: ?Ǥ??Þ¤? (???'@code{|this is a symbol|}')?? 78: @ref{Symbols}?⻲?Ȥ??Ʋ??????? 79: @c COMMON 80: 81: @c EN 82: @item Character literal by character code 83: Character literals can be written using its code, 84: by @code{#\x@var{NN}} (Gauche internal code) or @code{#\u@var{NN}} (Unicode). 85: @xref{Characters}, for details. 86: @c JP 87: @item ʸ?ú¥³¡¼?ɤˤ?ʸ???? 88: ʸ??????ú¥³¡¼?ɤˤ?ɽ?????뤳?Ȥ??Ǥ??Þ¤??? 89: Gauche???????ɤ?? @code{#\x@var{NN}} ?È¡? 90: Unicode??? @code{#\u@var{NN}}?Ȥ???ɽ???????????? 91: @ref{Characters}?ò»²¾È¤??Ʋ??????? 92: @c COMMON 93: 94: @c EN 95: @item Extended sharp syntax 96: Many more special tokens begins with '@code{#}' are defined. 97: See the table below. 98: @c JP 99: @item ??????줿#??ʸ 100: '@code{#}' ?ǻϤޤ??칽ʸ?????????????????Þ¤??? 101: ??????ò»²¾È¤??Ʋ??????? 102: @c COMMON 103: @end table 104: 105: @c EN 106: The table below lists sharp-syntaxes. 107: @c JP 108: ?ʲ?????Ï¥??㡼?×¹?ʸ?Υꥹ?ȤǤ??? 109: @c COMMON 110: 111: @multitable {aaaaaaaaaaaaaaaa} {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa} 112: @item @code{#!} 113: @c EN 114: @tab [SRFI-22] Specifies interpreter at the beginning of file. 115: Scheme loader ignores the line. 116: @c JP 117: @tab [SRFI-22] ?Õ¥??????Ƭ?Ç¥??󥿥ץ꥿??ꤷ?Þ¤??? 118: Scheme ?Î¥í¡¼??Ϥ??ιԤ?뤷?Þ¤??? 119: @c COMMON 120: @item @code{#"}, @code{##}, @code{#$}, @code{#%}, @code{#&}, @code{#'} 121: @c EN 122: @tab Unused. 123: @c JP 124: @tab ̤??Ñ¡? 125: @c COMMON 126: @item @code{#(} 127: @c EN 128: @tab [R5RS] Introduces a vector. 129: @c JP 130: @tab [R5RS] ?Ù¥?????????Þ¤??? 131: @c COMMON 132: @item @code{#)} 133: @c EN 134: @tab Unused. 135: @c JP 136: @tab ̤??Ñ¡? 137: @c COMMON 138: @item @code{#*} 139: @c EN 140: @tab If followed by a double quote, denotes an incomplete string. @xref{Strings}. 141: @c JP 142: @tab ??Ű???????³???Ȥ??Ï¡??????????ɽ???Þ¤???@ref{Strings}???È¡? 143: @c COMMON 144: @item @code{#+} 145: @c EN 146: @tab Unused. 147: @c JP 148: @tab ̤??Ñ¡? 149: @c COMMON 150: @item @code{#,} 151: @c EN 152: @tab [SRFI-10] Introduces reader constructor syntax. 153: @c JP 154: @tab [SRFI-10] ?꡼?????Ò¹?ʸ??????Þ¤??? 155: @c COMMON 156: @item @code{#-}, @code{#.} 157: @c EN 158: @tab Unused. 159: @c JP 160: @tab ̤??Ñ¡? 161: @c COMMON 162: @item @code{#/} 163: @c EN 164: @tab Introduces a literal regular expression. @xref{Regular expression}. 165: @c JP 166: @tab ?????????ɽ????????Þ¤???@ref{Regular expression}???È¡? 167: @c COMMON 168: @item @code{#0} @dots{} @code{#9} 169: @c EN 170: @tab [SRFI-38] Shared substructure definition and reference. 171: @c JP 172: @tab [SRFI-38] ??Í???Ö¥??ȥ饯??????È»??È¡? 173: @c COMMON 174: @item @code{#:} 175: @c EN 176: @tab Unused 177: @c JP 178: @tab ̤??Ñ¡? 179: @c COMMON 180: @item @code{#;} 181: @c EN 182: @tab [SRFI-62] S-expression comment. Reads next one S-expression and 183: discard it. 184: @c JP 185: @tab [SRFI-62] S???????È¡?³??S????Ȥ?ɤ?ΤƤޤ??? 186: @c COMMON 187: @item @code{#<} 188: @c EN 189: @tab Introduces an unreadable object. 190: @c JP 191: @tab ??ß¼????????Ö¥??????Ȥ?????Þ¤??? 192: @c COMMON 193: @item @code{#=}, @code{#>} 194: @c EN 195: @tab Unused. 196: @c JP 197: @tab ̤??Ñ¡? 198: @c COMMON 199: @item @code{#?} 200: @c EN 201: @tab Introduces debug macros. @xref{Debugging}. 202: @c JP 203: @tab ?ǥХå??Þ¥????Ƴ???Þ¤???@ref{Debugging}???È¡? 204: @c COMMON 205: @item @code{#@@} 206: @c EN 207: @tab Unused. 208: @c JP 209: @tab ̤??Ñ¡? 210: @c COMMON 211: @item @code{#a} 212: @c EN 213: @tab Unused. 214: @c JP 215: @tab ̤??Ñ¡? 216: @c COMMON 217: @item @code{#b} 218: @c EN 219: @tab [R5RS] Binary number prefix. 220: @c JP 221: @tab [R5RS] 2?Ê¿?????? 222: @c COMMON 223: @item @code{#c} 224: @c EN 225: @tab Unused. 226: @c JP 227: @tab ̤??Ñ¡? 228: @c COMMON 229: @item @code{#d} 230: @c EN 231: @tab [R5RS] Decimal number prefix. 232: @c JP 233: @tab [R5RS] 10?Ê¿?????? 234: @c COMMON 235: @item @code{#e} 236: @c EN 237: @tab [R5RS] Exact number prefix. 238: @c JP 239: @tab [R5RS] ??ο?????? 240: @c COMMON 241: @item @code{#f} 242: @c EN 243: @tab [R5RS] Boolean false, or 244: introducing SRFI-4 uniform vector. @xref{Uniform vectors}. 245: @c JP 246: @tab [R5RS] ?????ε??????뤤?? SRFI-4 ?Î¥??Õ¥???????????????Þ¤???@ref{Uniform vectors}????@c COMMON 247: @item @code{#g}, @code{#h} 248: @c EN 249: @tab Unused. 250: @c JP 251: @tab ̤??Ñ¡? 252: @c COMMON 253: @item @code{#i} 254: @c EN 255: @tab [R5RS] Inxact number prefix. 256: @c JP 257: @tab [R5RS] ???ο?????? 258: @c COMMON 259: @item @code{#j}, @code{#k}, @code{#l}, @code{#m}, @code{#n} 260: @c EN 261: @tab Unused. 262: @c JP 263: @tab ̤??Ñ¡? 264: @c COMMON 265: @item @code{#o} 266: @c EN 267: @tab [R5RS] Octal number prefix. 268: @c JP 269: @tab [R5RS] 8?Ê¿?????? 270: @c COMMON 271: @item @code{#p}, @code{#q}, @code{#r} 272: @c EN 273: @tab Unused. 274: @c JP 275: @tab ̤??Ñ¡? 276: @c COMMON 277: @item @code{#s} 278: @c EN 279: @tab [SRFI-4] introducing SRFI-4 uniform vector. @xref{Uniform vectors}. 280: @c JP 281: @tab [SRFI-4] SRFI-4 ?Î¥??Õ¥???????????????Þ¤???@ref{Uniform vectors}???È¡? 282: @c COMMON 283: @item @code{#t} 284: @c EN 285: @tab [R5RS] Boolean true. 286: @c JP 287: @tab [R5RS] ?????ο??? 288: @c COMMON 289: @item @code{#u} 290: @c EN 291: @tab [SRFI-4] introducing SRFI-4 uniform vector. @xref{Uniform vectors}. 292: @c JP 293: @tab [SRFI-4] SRFI-4 ?Î¥??Õ¥???????????????Þ¤???@ref{Uniform vectors}???È¡? 294: @c COMMON 295: @item @code{#v}, @code{#w} 296: @c EN 297: @tab Unused. 298: @c JP 299: @tab ̤??Ñ¡? 300: @c COMMON 301: @item @code{#x} 302: @c EN 303: @tab [R5RS] Hexadecimal number prefix. 304: @c JP 305: @tab [R5RS] 16?Ê¿???? 306: @c COMMON 307: @item @code{#y}, @code{#z} 308: @c EN 309: @tab Unused. 310: @c JP 311: @tab ̤??Ñ¡? 312: @c COMMON 313: @item @code{#[} 314: @c EN 315: @tab Introduces a literal character set. @xref{Character set}. 316: @c JP 317: @tab ???????????????Þ¤???@ref{Character set}????@c COMMON 318: @item @code{#\} 319: @c EN 320: @tab [R5RS] Introduces a literal character. @xref{Characters}. 321: @c JP 322: @tab [R5RS] ?????????Ƴ???Þ¤???@ref{Characters}???È¡? 323: @c COMMON 324: @item @code{#]}, @code{#^}, @code{#_} 325: @c EN 326: @tab Unused. 327: @c JP 328: @tab ̤??Ñ¡? 329: @c COMMON 330: @item @code{#`} 331: @c EN 332: @tab Introduces an interpolated string. @xref{String interpolation}. 333: @c JP 334: @tab ??Ö¤??줿ʸ???Ƴ???Þ¤???@ref{String interpolation}???È¡? 335: @c COMMON 336: @item @code{#@{} 337: @c EN 338: @tab Unused. 339: @c JP 340: @tab ̤??Ñ¡? 341: @c COMMON 342: @item @code{#|} 343: @c EN 344: @tab [SRFI-30] Introduces a block comment. Comment ends by matching '@code{|#}'. 345: @c JP 346: @tab [SRFI-30] ?Ö¥????????Ȥ?????Þ¤????????Ȥ?б?????@code{|#}' ?ǽ??????? 347: @c COMMON 348: @item @code{#@}}, @code{#~} 349: @c EN 350: @tab Unused. 351: @c JP 352: @tab ̤??Ñ¡? 353: @c COMMON 354: @end multitable 355: 356: @c ---------------------------------------------------------------------- 357: @node Literals, Making Procedures, Lexical structure, Core syntax 358: @section Literals 359: @c NODE ???? 360: 361: @defspec quote datum 362: [R5RS] 363: @c EN 364: Evaluates to @var{datum}. 365: @c JP 366: @var{datum}???켫?Ȥ???Þ¤??? 367: @c COMMON 368: @example 369: (quote x) @result{} x 370: (quote (1 2 3)) @result{} (1 2 3) 371: @end example 372: @end defspec 373: 374: @deftp {Reader Syntax} @code{'@var{datum}} 375: [R5RS] 376: @c EN 377: Equivalent to @code{(quote datum)}. 378: @c JP 379: @code{(quote datum)}??????Ǥ??? 380: @c COMMON 381: @example 382: 'x @result{} x 383: '(1 2 3) @result{} (1 2 3) 384: @end example 385: @end deftp 386: 387: @c EN 388: @emph{Note:} R5RS says it is an error to alter the value of 389: a literal expression. 390: Gauche doesn't check constant-ness of pairs and vectors, and does 391: not signal an error if you modify them using the destructive 392: procedures such as @code{set-car!} 393: and @code{vector-set!}. Doing so will cause unexpected results. 394: Gauche does check constant-ness of strings, and signals an error 395: if you try to alter a constant string. 396: @c JP 397: @emph{?:} R5RS?Ï¡?????????ͤ???????Ï¥??顼?Ǥ??????Ƥ??Þ¤??? 398: ??????Gauche?Ï¥Ú¥??È¥Ù¥????ˤĤ??ƤϤ??줬?????뤫?ɤ???????Ã¥????Ƥ??餺?? 399: @code{set-car!}??ode{vector-set!}???˲?????ˤ??Ƥ?????ͤ??????Ƥ⥨?顼?????????󡣤?????????????????Ǥ??? 400: ʸ???ؤ??Ƥ?????ɤ????Î¥???Ã¥???äƤ??ê¡¢??????????褦?Ȥ??? 401: ???顼????𤵤????? 402: @c COMMON 403: 404: @c ---------------------------------------------------------------------- 405: @node Making Procedures, Assignments, Literals, Core syntax 406: @section Making Procedures 407: @c NODE ???????@defspec lambda formals body @dots{} 408: [R5RS] 409: @c EN 410: Evaluates to a procedure. The environment in effect when this expression 411: is evaluated is stored in the procedure. When the procedure 412: is called, @var{body} is evaluated sequentially in the 413: stored environment extended by the bindings of the formal arguments, 414: and returns the value(s) of the last expression in the body. 415: @c JP 416: ???μ??????????ȼ?????????Þ¤??????μ???ɾ????줿????δĶ??????????Ý»?Þ¤??? 417: ???????ƤФ??È¡????????줿?Ķ??˰?«???ä????Ķ????var{body} ????ɾ????ì¡¢ 418: ?Ǹ?????ͤ?????????? 419: @c COMMON 420: 421: @c EN 422: @var{Formals} should have one of the following forms: 423: @itemize @bullet 424: @item 425: @code{(@var{variable} @dots{})} : The procedure takes a fixed number of 426: arguments. The actual arguments are bound to the corresponding 427: variables. 428: @item 429: @code{@var{variable}} : The procedure takes any number of arguments. 430: The actual arguments are collected to form a new list and bound to 431: the variable. 432: @item 433: @code{(@var{variable_0} @dots{} @var{variable_N-1}. @var{variable_N})} : 434: The procedure takes at least @var{N} arguments. The actual arguments 435: up to @var{N} is bound to the corresponding variables. 436: If more than @var{N} arguments are given, the rest arguments are 437: collected to form a new list and bound to @var{variable_N}. 438: @end itemize 439: @c JP 440: @var{Formals} ?ϰʲ??Τ????줫?η?Ǥʤ????ʤ?????@itemize @bullet 441: @item 442: @code{(@var{variable} @dots{})} : ?????Ï·??ä???????Þ¤??? 443: ?°????줾????????ar{variable}???????????? 444: @item 445: @code{@var{variable}} : ?????????ΰ????Þ¤??? 446: ?°????????ꥹ?Ȥ˽??????????Υꥹ?Ȥ?@var{varible}???????????? 447: @item 448: @code{(@var{variable_0} @dots{} @var{variable_N-1}. @var{variable_N})} : 449: ?????Ͼ??ʤ??Ȥ?ar{N}?Ĥΰ????Þ¤???@var{N}?ĤޤǤμ°????????ar{variable} 450: ????????????@var{N}?Ĥ?????°????????ꥹ?Ȥ˽????? 451: @var{variable_N}???????????? 452: @end itemize 453: @c COMMON 454: 455: @example 456: (lambda (a b) (+ a b)) 457: @c EN 458: @result{} @r{procedure that adds two arguments} 459: @c JP 460: @result{} @r{??Ĥΰ??û?????³??} 461: @c COMMON 462: 463: ((lambda (a b) (+ a b)) 1 2) 464: @result{} 3 465: @end example 466: @end defspec 467: 468: @c EN 469: @emph{Note:} Some Scheme implementations extend the syntax of @var{formals} 470: to have notation of optional arguments or keyword arguments, similar to 471: CommonLisp's. Gauche doesn't have such extensions now. 472: @c JP 473: @emph{?:} ?????Ĥ???cheme?μ???var{formals}?ι?ʸ??????Æ¡? 474: CommonLisp?Τ褦?˾?????????????ɰ??ɤ????????ˤ??Ƥ????Τ? 475: ????????Gauche?ˤϺ??ΤȤ??í¤½?Τ褦?ʳ???Ϥ???????@c COMMON 476: 477: @defmac cut expr-or-slot expr-or-slot2 @dots{} 478: @defmacx cute expr-or-slot expr-or-slot2 @dots{} 479: [SRFI-26] 480: @c EN 481: Convenience macros to notate a procedure compactly. 482: This form can be used to realize partial application, 483: a.k.a sectioning or projection. 484: @c JP 485: ????????ñ¤±¤???Ê¥Þ¥??????? 486: ??????ʬŬ???????뤿???Ȥ??Þ¤??? 487: @c COMMON 488: 489: @c EN 490: Each @var{expr-or-slot} must be either an expression or a symbol @code{<>}, 491: indicating a 'slot'. 492: The last @var{expr-or-slot} can be a symbol @code{<...>}, 493: indicating a 'rest-slot'. 494: @code{Cut} expands into a @code{lambda} form that takes as many arguments 495: as the number of slots in the given form, and 496: whose body is an expression 497: @example 498: (@var{expr-or-slot} @var{expr-or-slot2} @dots{}) 499: @end example 500: where each occurrence of @code{<>} is replaced to the corresponding 501: argument. 502: In case there is a rest-slot symbol, the resulting procedure is also 503: of variable arity, and all the extra arguments are passed 504: to the call of @var{expr-or-slot}. See the fourth example below. 505: @c JP 506: ??var{expr-or-slot}?ϼ??Þ¤??Ï¥????ode{<>}?Ǥʤ????ʤ??????Ǹ?@var{expr-or-slot}?Ï¥????ode{<...>}?Ǥ??äƤ⹽???Þ¤???@code{cut}?Ï¡????Î¥Õ¥??????ˤ???ode{<>}?????????? 507: @code{lambda}?Õ¥?????Ÿ?????????????Î¥Õ¥??????ܥǥ??ˤϼ??μ???????????? 508: @example 509: (@var{expr-or-slot} @var{expr-or-slot2} @dots{}) 510: @end example 511: â??????code{<>}??б????ë²¾????????????Þ¤??? 512: ?⤷?????ode{<...>}??Í¿?????Ƥ???????Ÿ?????줿@code{lambda}?Õ¥????? 513: ?????????????Ȥʤꡢ??????????????????????٤Ƥΰ? 514: @var{expr-or-slot}??ӽФ??Τ˻Ȥ??Þ¤?(????????????ȤΤ????? 515: @c COMMON 516: 517: @example 518: (cut cons (+ a 1) <>) @equiv{} (lambda (x2) (cons (+ a 1) x2)) 519: (cut list 1 <> 3 <> 5) @equiv{} (lambda (x2 x4) (list 1 x2 3 x4 5)) 520: (cut list) @equiv{} (lambda () (list)) 521: (cut list 1 <> 3 <...>) 522: @equiv{} (lambda (x2 . xs) (apply list 1 x2 3 xs)) 523: (cut <> a b) @equiv{} (lambda (f) (f a b)) 524: 525: ;; Usage 526: (map (cut * 2 <>) '(1 2 3 4)) 527: (for-each (cut write <> port) exprs) 528: @end example 529: 530: @c EN 531: @code{Cute} is a variation of @code{cut} that evaluates @code{expr-or-slot}s 532: before creating the procedure. 533: @c JP 534: @code{cute}??code{cut}?Ȥۤ?????Ǥ?????@code{expr-or-slot}????????? 535: ?Õ¥????ब???????????????????ۤʤ????? 536: @c COMMON 537: 538: @example 539: (cute cons (+ a 1) <>) 540: @equiv{} (let ((xa (+ a 1))) (lambda (x2) (cons xa x2))) 541: @end example 542: 543: @c EN 544: Gauche also has a built-in procedure @code{pa$} for partial application 545: (@xref{Combinators}). 546: @c JP 547: Gauche?Ϥޤ????ʬŬ????@code{pa$}?Ȥ?????ß¹??ß¼?????äƤ??Þ¤? 548: (@xref{Combinators}?????? 549: @c COMMON 550: @end defmac 551: 552: @defmac case-lambda clause @dots{} 553: [SRFI-16] 554: @c EN 555: Each @var{clause} should have the form (@var{formals} @var{expr} @dots{}), 556: where @var{formals} is a formal arguments list as for @code{lambda}. 557: 558: This expression evaluates to a procedure that accepts a variable 559: number of arguments and is lexically scoped in the same manner as 560: procedures resulting from @code{lambda} expressions. 561: When the procedure is called with some arguments, 562: then the first @var{clause} for which the arguments agree with 563: @var{formals} is selected, where agreement is specified as for 564: the @var{formals} of a @code{lambda} expression. 565: The variables of @code{formals} are bound to the given 566: arguments, and the @var{expr} @dots{} are evaluated within the environment. 567: 568: It is an error for the arguments not to agree with 569: the @var{formals} of any @var{clause}. 570: @c JP 571: ???줾?? @var{clause} ??(@var{formals} @var{expr} @dots{}) ?Ȥ?????? 572: ?Ǥʤ????ʤ????󡣤????Ç¡?@var{formals} ??@code{lambda} ?β???ꥹ?ȤǤ??? 573: 574: ???μ??Ï¡?ɾ??????Ȳ?Ñ¿???Ȥꡢ@code{lambda} ???????̤Ȥ????Ǥ???³??Ʊ???ì¥?????륹?????פ?ļ????ˤʤ????????μ????? 575: ?????Ĥ??ΰ??Ȥ??ƤФ??È¡?@var{formals} ?Ȱ???????б?????ǽ? @var{clause} ???????????????ΰ????Ȥ????Τϡ? 576: @code{lambda} ????@var{formals} ??б??????ΤȤ??Æ»?ꤵ?????Ǥ??? 577: @code{formals} ??Ñ¿?°?«????ì¡¢ @var{expr} @dots{} ???????Ķ?????????????? 578: 579: ?ºݤΰ??ɤ?@var{clause} ??@var{formals} ?ˤ?????ʤ??????顼?Ǥ??? 580: @c COMMON 581: @end defmac 582: 583: @c ---------------------------------------------------------------------- 584: @node Assignments, Conditionals, Making Procedures, Core syntax 585: @section Assignments 586: @c NODE ?? 587: 588: @defspec set! symbol expression 589: @defspecx set! (proc arg @dots{}) expression 590: [R5RS][SRFI-17] 591: @c EN 592: First, @var{expression} is evaluated. 593: In the first form, the binding of @var{symbol} is modified 594: so that next reference of @var{symbol} will return 595: the result of @var{expression}. 596: If @var{symbol} is not locally bound, the global variable named @var{symbol} 597: must already exist, or an error is signalled. 598: @c JP 599: ?Þ¤?@var{expression}??ɾ??????????ǽ?????Ǥϡ?@var{symbol}?ؤ????? 600: @var{expression}?η?????褦??ѹ????????? 601: ?⤷@var{symbol}???í¡¼????«????????ʤ????Ï¡????í¡¼?Ð¥????r{symbol}?? 602: ¸?ߤ??Ƥ??ʤ????ʤ????󡣤????Ǥʤ??????顼????𤵤????? 603: @c COMMON 604: 605: @c EN 606: The second form is a ``generalized set!'' specified in SRFI-17. 607: It is a syntactic sugar of the following form. 608: @example 609: ((setter @var{proc}) @var{arg} @dots{} @var{expression}) 610: @end example 611: Note the order of the arguments of the setter method 612: differs from CommonLisp's @code{setf}. 613: 614: Some examples: 615: @c JP 616: 2???η????RFI-17??????????????????줿set!?פǤ??? 617: ??????ʸŪ???Ǥ??ê¡¢?ºݤϰʲ??Τ褦?˲???????? 618: @example 619: ((setter @var{proc}) @var{arg} @dots{} @var{expression}) 620: @end example 621: CommonLisp??code{setf}?Ȥϡ?setter??????Ϥ??????????ۤʤ뤳?Ȥ?????Ʋ??????? 622: 623: ??? 624: @c COMMON 625: 626: @example 627: (define x 3) 628: (set! x (list 1 2)) @result{} @r{undefined} 629: x @result{} (1 2) 630: 631: (set! (car x) 5) @result{} @r{undefined} 632: x @result{} (5 2) 633: @end example 634: @end defspec 635: 636: @defmac set!-values (var @dots{}) expr 637: @c EN 638: Sets values of multiple variables at once. @var{Expr} must yield 639: as many values as @var{var} @dots{}. Each value is set to 640: the corresponding @var{var}. 641: @c JP 642: Ê£??Ñ¿?????˹??????Þ¤???@var{Expr}??????????????? 643: ?????ͤ?????ʤ??Фʤ????ó¡£³?ͤ????????ar{var}?????åȤ??????? 644: @c COMMON 645: @example 646: (define a 0) 647: (define b 1) 648: (set!-values (a b) (values 3 4)) @result{} @r{undefined} 649: a @result{} 3 650: b @result{} 4 651: (set!-values (a b) (values b a)) @result{} @r{undefined} 652: a @result{} 4 653: b @result{} 3 654: @end example 655: @end defmac 656: 657: @defun setter proc 658: [SRFI-17] 659: @c EN 660: Returns a setter procedure associated to the procedure @var{proc}. 661: If no setter is associated to @var{proc}, its behavior is undefined. 662: 663: A setter procedure @var{g} of a procedure @var{f} is such that when used 664: as @code{(g a b @dots{} v)}, the next evaluation of @code{(f a b @dots{})} 665: returns @var{v}. 666: 667: To associate a setter procedure to another procedure, you can use 668: the setter of @code{setter}, like this: 669: @example 670: (set! (setter @var{f}) @var{g}) 671: @end example 672: 673: A procedure's setter can be ``locked'' to it. System default setters, 674: like @code{set-car!} for @code{car}, is locked and can't be set by 675: the above way. In order to lock a setter to a user defined procedure, 676: use @code{getter-with-setter} below. 677: 678: If @var{proc} is not a procedure, a setter generic function 679: of @code{object-apply} is returned; 680: it allows the applicable object extension to work 681: seamlessly with the generalized @code{set!}. 682: @xref{Applicable objects}, for the details. 683: @c JP 684: ????@var{proc}??etter????????Þ¤??? 685: @var{proc}??setter????????ʤ????????̤???Ǥ??? 686: 687: ??????r{f}??etter????@var{g}?Ȥϡ??⤷@code{(g a b @dots{} v)}?Τ褦???ƤФ줿????????code{(f a b @dots{})}??@var{v}????褦?ˤʤ?³???Τ??ȤǤ??? 688: 689: ????³????etter?????????????Ï¡?@code{setter}???Ȥ?etter??????????Ȥ? 690: ?Ǥ??Þ¤??? 691: @example 692: (set! (setter @var{f}) @var{g}) 693: @end example 694: 695: ???μ?????etter??????????פ??뤳?Ȥ??Ǥ??Þ¤????????Æ¥????etter?????? 696: ?????code{car}??Ф???ode{set-car!}??Ï¥??????????Æ¡??åµ?Τ褦?????????뤳?ȤϽ?????󡣥桼??????????etter??????Ã¥??????ϲ?????@code{getter-with-setter}????Þ¤??? 697: 698: @var{proc}???????Ǥʤ????Ï¡?@code{object-apply}?????Í¥????Õ¥??󥯥?????setter???????????????????Ŭ???????Ö¥??????Ȥ????????줿@code{set!} 699: ?Ǥ⤦?Þ¤?ư??Þ¤????ܤ?????ref{Applicable objects}?ò»²¾È¤??Ʋ??????? 700: @c COMMON 701: @end defun 702: 703: @defun has-setter? proc 704: @c EN 705: Returns @code{#t} if a setter is associated to @var{proc}. 706: @c JP 707: @var{proc}??setter??????äƤ?????@code{#t}????Þ¤??? 708: @c COMMON 709: @end defun 710: 711: @defun getter-with-setter get set 712: [SRFI-17] 713: @c EN 714: Takes two procedure @var{get} and @var{set}. Returns a new 715: procedure which does the same thing as @var{get}, and its setter is 716: locked to @var{set}. 717: 718: The intention of this procedure is, according to the SRFI-17 document, 719: to allow implementations to inline setters efficiently. Gauche 720: hasn't implement such optimization yet. 721: @c JP 722: 2?Ĥμ?????@var{get}??var{set}??ê¡¢??????????????Þ¤??? 723: ????????????var{get}?????ư??????????etter??????var{set}?Ë¥??????? 724: ???Þ¤??? 725: 726: ???μ????Ï¡?SRFI-17?ˤ??С?setter?????Î¥???????????ˤ??뤿?? 727: ???Ǥ?????Gauche?ǤϤޤ???Τ褦?ʺ?????ϼ?õ¤µ¤????Þ¤???@c COMMON 728: @end defun 729: 730: @c EN 731: A few macros that adopts the same semantics of generalized @code{set!} 732: are also provided. They are built on top of @code{set!}. 733: @c JP 734: Gauche?Ǥϡ????????줿@code{set!}????????Þ¥????????Ĥ????Ĥ??Î¥Þ¥??í¤¬??????????Þ¤?????????var{set!}??ä??????????????? 735: @c COMMON 736: 737: @defmac push! place item 738: @c EN 739: Conses @var{item} 740: and the value of @var{place}, then sets the result to @var{place}. 741: @var{place} is either a variable or a form @var{(proc arg @dots{})}, 742: as the second argument of @code{set!}. The result of this form 743: is undefined. 744: @c JP 745: @var{item}??var{place}??ͤò¥³¥ó¥¹¤??????η????var{place}?Ë¥??åȤ??Þ¤??? 746: @var{place}??code{set!}?????Ë¡? 747: ???var{(proc arg @dots{})}?Ȥ???????Ǥʤ????ʤ????󡣤??Î¥Þ¥????????̤???Ǥ??? 748: @c COMMON 749: 750: @example 751: (define x (list 2)) 752: (push! x 3) @result{} @r{undefined} 753: x @result{} (3 2) 754: 755: (push! (cdr x) 4) @result{} @r{undefined} 756: x @result{} (3 4 2) 757: @end example 758: 759: @c EN 760: When @var{place} is a list, it roughly expands like the following. 761: @c JP 762: @var{place}???ꥹ?Ȥξ??????????????Õ¤?????????????? 763: @c COMMON 764: 765: @example 766: (push! (foo x y) item) 767: @equiv{} 768: (let ((tfoo foo) 769: (tx x) 770: (ty y)) 771: ((setter tfoo) tx ty (cons item (tfoo tx ty)))) 772: @end example 773: 774: @c EN 775: Note: Common Lisp's @code{push} macro takes its argument reverse 776: order. I adopted this order since it is consistent with other 777: destructive operations. Perl's @code{push} function takes the same 778: argument order, but it appends @var{item} at the end of the array 779: (Perl's @code{unshift} is closer to @code{push!}). 780: You can use a queue (@xref{Queue}) if you need a behavior of Perl's 781: @code{push}. 782: @c JP 783: ???Common Lisp??code{push}?Þ¥??????դν??Ǽ??Þ¤??? 784: @code{push!}????????Ä·???Ȥθߴ??????Ƥ??ν??Ȥ??Þ¤????? 785: Perl??code{push}?Ø¿?code{push!}?Þ¥???Ʊ?????Ǥ????? 786: @var{item}?Ï¥??????󥹤???ɲä????? (Perl??code{unshift}???ode{push!}??????ᤤ?Ǥ?)?? 787: Perl??ush???ڥ졼??????ɬ??ʤ?eue???Ȥ??Þ¤? (@ref{Queue}?????? 788: @c COMMON 789: @end defmac 790: 791: @defmac pop! place