Basic Data Extensions
3.1 Void and Undefined
MzScheme returns the unique void value -- printed as
#<void> -- for expressions that have unspecified results in
R5RS. The procedure takes any
number of arguments and returns void:
void
Variables bound by letrec-values that are accessible but
not yet initialized are bound to the unique undefined
value, printed as #<undefined>.
3.2 Booleans
Unless otherwise specified, two instances of a particular MzScheme
data type are only when they are equal?. Two
values are eq? only when they are either eqv?,
both eq?+nan.0, or both = and have the same exactness
and sign. (The inexact numbers 0.0 and -0.0 are
not , although they are eqv?=.)
The and andmap procedures apply a test
procedure to the elements of a list, returning immediately when the
result for testing the entire list is determined. The arguments to
ormap and andmap are the same as for ormap, but a
single boolean value is returned as the result, rather than a list:
map
(andmapproc list···1)appliesprocto elements of thelists from the first elements to the last, returning#fas soon as any application returns#f. If no application ofprocreturns#f, then the result of the last application ofprocis returned. If thelists are empty, then#tis returned.(ormapproc list···1)appliesprocto elements of thelists from the first elements to the last. If any application returns a value other than#f, that value is immediately returned as the result of theapplication. If all applications oformapprocreturn#f, then the result is#f. If thelists are empty, then#fis returned.
Examples:
(andmappositive?'(1 2 3)) ; =>#t(ormapeq?'(a b c) '(a b c)) ; =>#t(andmappositive?'(1 2 a)) ; => raisesexn:fail:contract(ormappositive?'(1 2 a)) ; =>#t(andmappositive?'(1 -2 a)) ; =>#f(andmap+ '(1 2 3) '(4 5 6)) ; =>9(ormap+ '(1 2 3) '(4 5 6)) ; =>5
3.3 Numbers
A number in MzScheme is one of the following:
a fixnum exact integer (30 bits4 plus a sign bit)
a bignum exact integer (cannot be represented in a fixnum)
a fraction exact rational (represented by two exact integers)
a flonum inexact rational (double-precision floating-point number)
a complex number; either the real and imaginary parts are both exact or inexact, or the number has an exact zero real part and an inexact imaginary part; a complex number with an inexact zero imaginary part is a real number
MzScheme extends the number syntax of R5RS in three ways:
All input radixes (
#b,#o,#d, and#x) allow ``decimal'' numbers that contain a period or exponent marker. For example,#b1.1is equivalent to1.5. In hexadecimal numbers,andedalways stand for a hexadecimal digit, not an exponent marker.The mantissa of a number with an exponent marker can be expressed as a fraction. For example,
1/2e3is equivalent to500.0, and1/2e2+1/2e4iis equivalent to50.0+5000.0i.The following are inexact numerical constants:
+inf.0(infinity),-inf.0(negative infinity),+nan.0(not a number), and-nan.0(same as+nan.0). These names can also be used within complex constants, as in-inf.0+inf.0i. These names are case-insensitive.
The special inexact numbers +inf.0, -inf.0, and
+nan.0 have no exact form. Dividing by an inexact zero returns
+inf.0 or -inf.0, depending on the sign of the
dividend. The infinities are integers, and they answer #t for
both even? and odd?. The +nan.0 value is not an
integer and is not = to itself, but +nan.0 is
eqv? to itself.5 Similarly, (= 0.0 -0.0)
is #t, but ( is eqv? 0.0 -0.0)#f.
All multi-argument arithmetic procedures operate pairwise on arguments from left to right.
The string->number procedure works on all number
representations and exact integer radix values in the range 2
to 16 (inclusive). The number->string procedure
accepts all number types and the radix values 2, 8,
10, and 16; however, if an inexact number is provided
with a radix other than 10, the
exn:fail:contract exception is raised.
The and add1 procedures work on any number:
sub1
The following procedures work on integers:
(quotient/remaindern1 n2)returns two values:(andquotientn1 n2)(.remaindern1 n2)(integer-sqrtn)returns the integer square-root ofn. For positiven, the result is the largest positive integer bounded by the(. For negativesqrtn)n, the result is(* (.integer-sqrt(- n)) 0+i)(integer-sqrt/remaindern)returns two values:(andinteger-sqrtn)(- n (.expt(integer-sqrtn) 2))
The following procedures work on exact integers in their (semi-infinite) two's complement representation:
(bitwise-iorn···)returns the bitwise ``inclusive or'' of thens. If no arguments are provided, the result is0.(bitwise-andn···)returns the bitwise ``and'' of thens. If no arguments are provided, the result is-1.(bitwise-xorn···)returns the bitwise ``exclusive or'' of thens. If no arguments are provided, the result is0.(bitwise-notn)returns the bitwise ``not'' ofn.(arithmetic-shiftn m)returns the bitwise ``shift'' ofn. The integernis shifted left bymbits; i.e.,mnew zeros are introduced as rightmost digits. Ifmis negative,nis shifted right by -mbits; i.e., the rightmostmdigits are dropped.
The procedure generates pseudo-random numbers:
random
(randomk)returns a random exact integer in the range0tok- 1 wherekis an exact integer between 1 and 231 - 1, inclusive. The number is provided by the current pseudo-random number generator, which maintains an internal state for generating numbers.6(random)returns a random inexact number between0and1, exclusive, using the current pseudo-random number generator.(random-seedk)seeds the current pseudo-random number generator withk, an exact integer between 0 and 231 - 1, inclusive. Seeding a generator sets its internal state deterministically; seeding a generator with a particular number forces it to produce a sequence of pseudo-random numbers that is the same across runs and across platforms.(pseudo-random-generator->vectorgenerator)produces a vector that represents the complete internal state ofgenerator. The vector is suitable as an argument tovector->pseudo-random-generatorto recreate the generator in its current state (across runs and across platforms).(vector->pseudo-random-generatorvec)produces a pseudo-random number generator whose internal state corresponds tovec. The vectorvecmust contain six exact integers; the first three integers must be in the range0to4294967086, inclusive; the last three integers must be in the range0to4294944442, inclusive; at least one of the first three integers must be non-zero; and at least one of the last three integers must be non-zero.(current-pseudo-random-generator)returns the current pseudo-random number generator, and(current-pseudo-random-generatorgenerator)sets the current generator togenerator. See also section 7.9.1.10.(make-pseudo-random-generator)returns a new pseudo-random number generator. The new generator is seeded with a number derived from(.current-milliseconds)(pseudo-random-generator?v)returns#tifvis a pseudo-random number generator,#fotherwise.
The following procedures convert between Scheme numbers and common machine byte representations:
(integer-bytes->integerstring signed?[big-endian?])converts the machine-format number encoded instringto an exact integer. Thestringmust contain either 2, 4, or 8 characters. Ifsigned?is true, then the string is decoded as a two's-complement number, otherwise it is decoded as an unsigned integer. Ifbig-endian?is true, then the first character's ASCII value provides the most significant eight bits of the number, otherwise the first character provides the least-significant eight bits, and so on. The default value ofbig-endian?is the result of.system-big-endian?(integer->integer-bytesn size-n signed?[big-endian? to-string])converts the exact integernto a machine-format number encoded in a string of lengthsize-n, which must be 2, 4, or 8. Ifsigned?is true, then the number is encoded with two's complement, otherwise it is encoded as an unsigned bit stream. Ifbig-endian?is true, then the most significant eight bits of the number are encoded in the first character of the resulting string, otherwise the least-significant bits are encoded in the first character, and so on. The default value ofbig-endian?is the result of.system-big-endian?If
to-stringis provided, it must be a mutable string of lengthsize-n; in that case, the encoding ofnis written intoto-string, andto-stringis returned as the result. Ifto-stringis not provided, the result is a newly allocated string.If
ncannot be encoded in a string of the requested size and format, theexn:fail:contractexception is raised. Ifto-stringis provided and it is not of lengthsize-n, theexn:fail:contractexception is raised.(floating-point-bytes->realstring[big-endian?])converts the IEEE floating-point number encoded instringto an inexact real number. Thestringmust contain either 4 or 8 characters. Ifbig-endian?is true, then the first character's ASCII value provides the most significant eight bits of the IEEE representation, otherwise the first character provides the least-significant eight bits, and so on. The default value ofbig-endian?is the result of.system-big-endian?(real->floating-point-bytesx size-n[big-endian? to-string])converts the real numberxto its IEEE representation in a string of lengthsize-n, which must be 4 or 8. Ifbig-endian?is true, then the most significant eight bits of the number are encoded in the first character of the resulting string, otherwise the least-significant bits are encoded in the first character, and so on. The default value ofbig-endian?is the result of.system-big-endian?If
to-stringis provided, it must be a mutable string of lengthsize-n; in that case, the encoding ofnis written intoto-string, andto-stringis returned as the result. Ifto-stringis not provided, the result is a newly allocated string.If
to-stringis provided and it is not of lengthsize-n, theexn:fail:contractexception is raised.(system-big-endian?)returns#tif the native encoding of numbers is big-endian for the machine running MzScheme,#fif the native encoding is little-endian.
3.4 Characters
MzScheme characters range over Unicode scalar values (see
section 1.2.1), which includes characters whose values range from
#x0 to #x10FFFF, but not including #xD800
to #xDFFF. The procedure char->integer returns a
character's code-point number, and integer->char converts
a code-point number to a character. If integer->char is
given an integer that is either outside #x0 to
#x10FFFF or in the excluded range #xD800 to
#xDFFF, the exn:fail:contract exception is raised.
Character constants include special named characters, such as
#\newline, plus octal representations (e.g.,
#\251), and Unicode-style hexadecimal representations (e.g.,
#\u03BB). See section 11.2.4 for more information on
character constants.
The character comparison
procedures char=?, char<?, char-ci=?,
etc. take two or more character arguments and check the arguments
pairwise (like the numerical comparison procedures). Two characters
are eq? whenever they are char=?. The
expression ( produces the same result
as char<? char1 char2)(< (, etc.
The case-independent char->integer char1) (char->integer char2))-ci procedures compare characters after
case-folding with char-foldcase (described below).
The character predicates produce results consistent with the Unicode database7 and (usually) SRFI-14. These procedures are fully portable; their results do not depend on the current platform or locale.
(char-alphabetic?char)-- returns#tifchar's Unicode general category is Lu, Ll, Lt, Lm, or Lo,#fotherwise.(char-lower-case?char)-- returns#tifcharhas the Unicode ``Lowercase'' property.(char-upper-case?char)-- returns#tifcharhas the Unicode ``Uppercase'' property.(char-title-case?char)-- returns#tifchar's Unicode general category is Lt,#fotherwise.(char-numeric?char)-- returns#tifchar's Unicode general category is Nd,#fotherwise.(char-symbolic?char)-- returns#tifchar's Unicode general category is Sm, Sc, Sk, or So,#fotherwise.(char-punctuation?char)-- returns#tifchar's Unicode general category is Pc, Pd, Ps, Pe, Pi, Pf, or Po,#fotherwise.(char-graphic?char)-- returns#tifchar's Unicode general category is Mn, Mc, Me, or if one of the following produces#twhen applied tochar:char-alphabetic?,char-numeric?,char-symbolic?, orchar-punctuation?.(char-whitespace?char)-- returns#tifchar's Unicode general category is Zs, Zl, or Zp, or ifcharis one of the following:#\tab,#\newline,#\vtab,#\page, or#\return.(char-blank?char)-- returns#tifchar's Unicode general category is Zs or ifcharis#\tab. (These correspond to horizontal whitespace.)(char-iso-control?char)-- return#tifcharis between#\u0000and#\u001Finclusive or#\u007Fand#\u009Finclusive.(char-general-categorychar)-- returns a symbol representing the character's Unicode general category, which is'lu,'ll,'lt,'lm,'lo,'mn,'mc,'me,'nd,'nl,'no,'ps,'pe,'pi,'pf,'pd,'pc,'po,'sc,'sm,'sk,'so,'zs,'zp,'zl,'cc,'cf,'cs,'co, or'cn.
Character conversions are also consistent with the 1-to-1 code point mapping defined by Unicode. String procedures (see section 3.5) handle the case where Unicode defines a locale-independent mapping from the code point to a code-point sequence (in addition to the 1-1 mapping on scalar values).
(char-upcasechar)produces a character according to the upcase mapping provided by the Unicode database forchar; ifcharhas no upcase mapping,char-upcaseproduceschar.(char-downcasechar)produces a character according to the downcase mapping provided by the Unicode database forchar; ifcharhas no downcase mapping,char-downcaseproduceschar.(char-titlecasechar)produces a character according to the titlecase mapping provided by the Unicode database forchar; ifcharhas no titlecase mapping,char-titlecaseproduceschar.(char-foldcasechar)produces a character according to the case-folding mapping provided by the Unicode database forchar.
(make-known-char-range-list) produces a list of three-element
lists, where each three-element list represents a set of consecutive
code points for which the Unicode standard specifies character
properties. Each three-element list contains two integers and a
boolean; the first integer is a starting code-point value
(inclusive), the second integer is an ending code-point value
(inclusive), and the boolean is #t when all characters in
the code-point range have identical results for all of the
character predicates above. The three-element lists are ordered in
the overall result list such that later lists represent larger
code-point values, and all three-element lists are separated from
every other by at least one code-point value that is not specified by
Unicode.
(char-utf-8-length char) produces the same result as
(.bytes-length (string->bytes/utf-8 (string char)))
3.5 Strings
Since a string consists of a sequence of characters, a string in MzScheme is a Unicode code-point sequence. MzScheme also provides byte strings, as well as functions to convert between byte strings and strings with respect to various encodings, including UTF-8 and the current locale's encoding. See section 1.2 for an overview of Unicode, locales, and encodings, and see section 3.6 for more specific information on byte-string conversions.
A string can be mutable or immutable. When an immutable string is
provided to a procedure like , the
string-set!exn:fail:contract exception is raised. String constants generated by
are immutable. read(string->immutable-string string)
returns an immutable string with the same content as string,
and it returns string itself if string is immutable. (See
also in section 3.10.)immutable?
(substring string start-k [end-k]) returns a mutable string, even
if the string argument is immutable. The end-k argument
defaults to (string-length string)
(string-copy! dest-string dest-start-k src-string [src-start-k src-end-k])
changes the characters of dest-string from positions
dest-start-k (inclusive) to dest-end-k (exclusive) to
match the characters in src-string from src-start-k
(inclusive). If src-start-k is not provided, it defaults to
0. If src-end-k is not provided, it defaults to
(. The strings string-length src-string)dest-string
and src-string can be the same string, and in that case the
destination region can overlap with the source region; the
destination characters after the copy match the source characters
from before the copy. If any of dest-start-k,
src-start-k, or src-end-k are out of range (taking into
account the sizes of the strings and the source and destination
regions), the exn:fail:contract exception is raised.
When a string is created with make-string without a fill
value, it is initialized with the null character (#\nul) in
all positions.
The string comparison
procedures string=?, string<?, string-ci=?,
etc. take two or more string arguments and check the arguments
pairwise (like the numerical comparison procedures). String
comparisons are performed through pairwise comparison of characters;
for the -ci operations, the two strings are first
case-folded using string-foldcase (described
below). Comparisons using all of these functions are fully
portable; the results do not depend on the current platform or
locale.
The following string-conversion procedures take into account Unicode's locale-independent conversion rules that map code-point sequences to code-point sequences (instead of simply mapping a 1-to-1 function on code points over the string). In each case, the string produced by the conversion can be longer than the input string.
(string-upcasestring)returns a string whose characters are the upcase conversion of the characters instring.(string-downcasestring)returns a string whose characters are the downcase conversion of the characters instring.(string-titlecasestring)returns a string where the first character in each sequence of cased characters instring(ignoring case-ignorable characters) is converted to titlecase, and all other cased characters are downcased.(string-foldcasestring)returns a string whose characters are the case-fold conversion of the characters instring.
Examples:
(string-upcase"abc!") ; =>"ABC!"(string-upcase"Stra\xDFe") ; =>"STRASSE"(string-downcase"aBC!") ; =>"abc!"(string-downcase"Stra\xDFe") ; =>"stra\xDFe"(string-downcase"\u039A\u0391\u039F\u03A3") ; =>"\u03BA\u03b1\u03BF\u03C2"(string-downcase"\u03A3") ; =>"\u03C3"(string-titlecase"aBC twO") ; =>"Abc Two"(string-titlecase"y2k") ; =>"Y2K"(string-titlecase"main stra\xDFe") ; =>"Main Stra\xDFe"(string-titlecase"stra \xDFe") ; =>"Stra Sse"(string-foldcase"aBC!") ; =>"abc!"(string-foldcase"Stra\xDFe") ; =>"strasse"(string-foldcase"\u039A\u0391\u039F\u03A3") ; =>"\u03BA\u03b1\u03BF\u03C3"
In addition to the character-based string procedures, MzScheme provides the following locale-sensitive procedures (see also section 1.2.2 and section 7.9.1.11):
(string-locale-upcasestring)-- may produce a string that is longer or shorter thanstringif the current locale has complex case-folding rules.(string-locale-downcasestring)-- likestring-locale-upcase, may produce a string that is longer or shorter thanstring
These procedures depend only on the current locale's case-conversion and collation rules, and not on its encoding rules.
MzScheme provides four Unicode-normalization procedures:
(string-normalize-nfdstring)-- returns a string that is the Unicode normalized form D ofstring.(string-normalize-nfkdstring)-- returns a string that is the Unicode normalized form KD ofstring.(string-normalize-nfcstring)-- returns a string that is the Unicode normalized form C ofstring.(string-normalize-nfkcstring)-- returns a string that is the Unicode normalized form KC ofstring.
For each of the normalization procedures, if the given string is already in the corresponding Unicode normal form, the string may be returned directly as the result (instead of a newly allocated string).
3.6 Byte Strings
A byte string is like a string, but it a sequence of bytes
instead of characters. A byte is an exact integer between
0 and 255 inclusive; (byte? v) produces
#t if v is such an exact integer, #f
otherwise. Two bytes strings are if they are bytewise
equal, and two byte strings are equal? only if they are
eqv?.eq?
MzScheme provides byte-string operations in parallel to the character-string operations:
A byte-string constant is written like a string, but prefixed with
# (with no space between # and the opening
double-quote). A byte-string constant can contain escape sequences,
as in #"\n", just like strings; an exn:fail:read exception
is raised if a ``\u'' sequence appears within a byte
string and the given hexadecimal value is larger than 255.
Like character strings, byte strings generated by are
immutable, and when an immutable string is provided to a procedure
like read, the bytes-set!exn:fail:contract exception is raised.
The following procedures convert between byte strings and character strings:
(bytes->string/utf-8bytes[err-char start-k end-k])-- produces a string by decoding thestart-ktoend-ksubstring ofbytesas a UTF-8 encoding of Unicode code points. Iferr-charis provided and not#f, then it is used for bytes that fall in the range#o200to#o377but are not part of a valid encoding sequence. (This is consistent with reading characters from a port; see section 11.1 for more details.) Iferr-charis#for not provided, and if thestart-ktoend-ksubstring ofbytesis not a valid UTF-8 encoding overall, then theexn:fail:contractexception is raised. Ifstart-korend-kare not provided, they default to0and(, respectively.bytes-lengthbytes)(bytes->string/localebytes[err-char start-k end-k])-- produces a string by decoding thestart-ktoend-ksubstring ofbytesusing the current locale's encoding (see also section 1.2.2). Iferr-charis provided and not#f, it is used for each byte inbytesthat is not part of a valid encoding; iferr-charis#for not provided, and if thestart-ktoend-ksubstring ofbytesis not a valid encoding overall, then theexn:fail:contractexception is raised. Ifstart-korend-kare not provided, they default to0and(, respectively.bytes-lengthbytes)(bytes->string/latin-1bytes[err-char start-k end-k])-- produces a string by decoding thestart-ktoend-ksubstring ofbytesas a Latin-1 encoding of Unicode code points; i.e., each byte is translated directly to a character usinginteger->char, so the decoding always succeeds.8 Theerr-charargument is ignored, but for consistency with the other operations, it must be a character or#fif provided. Ifstart-korend-kare not provided, they default to0and(, respectively.bytes-lengthbytes)(string->bytes/utf-8string[err-byte start-k end-k])-- produces a byte string by ending thestart-ktoend-ksubstring ofstringvia UTF-8 (always succeeding). Theerr-charargument is ignored, but for consistency with the other operations, it must be a byte or#fif provided. Ifstart-korend-kare not provided, they default to0and(, respectively.string-lengthstring)(string->bytes/localestring[err-byte start-k end-k])-- produces a string by encoding thestart-ktoend-ksubstring ofstringusing the current locale's encoding (see also section 1.2.2). Iferr-byteis provided and not#f, it is used for each character instringthat cannot be encoded for the current locale; iferr-byteis#for not provided, and if thestart-ktoend-ksubstring ofstringcannot be encoded, then theexn:fail:contractexception is raised. Ifstart-korend-kare not provided, they default to0and(, respectively.string-lengthstring)(string->bytes/latin-1string[err-byte start-k end-k])-- produces a string by encoding thestart-ktoend-ksubstring ofstringusing Latin-1; i.e., each character is translated directly to a byte usingchar->integer. Iferr-byteis provided and not#f, it is used for each character instringwhose value is greater than255;9 iferr-byteis#for not provided, and if thestart-ktoend-ksubstring ofstringhas a character with a value greater than255, then theexn:fail:contractexception is raised. Ifstart-korend-kare not provided, they default to0and(, respectively.string-lengthstring)(string-utf-8-lengthstring[start-k end-k])returns the length in bytes of the UTF-8 encoding ofstring's substring fromstart-ktoend-k, but without actually generating the encoded bytes. Ifstart-kis not provided, it defaults to0, andend-kdefaults to(.string-lengthstring)(bytes-utf-8-lengthbytes[err-char start-k end-k])returns the length in characters of the UTF-8 decoding ofbytes's substring fromstart-ktoend-k, but without actually generating the decoded characters. Ifstart-kis not provided, it defaults to0, andend-kdefaults to(. Ifbytes-lengthbytes)err-charis#fand the substring is not a UTF-8 encoding overall, the result is#f. Otherwise,err-charis used to resolve decoding errors as inbytes->string/utf-8.(bytes-utf-8-refbytes[skip-k err-char start-k end-k])returns theskip-kth character in the UTF-8 decoding ofbytes's substring fromstart-ktoend-k, but without actually generating the other decoded characters. Ifstart-kis not provided, it defaults to0, andend-kdefaults to(. If the substring is not a UTF-8 encoding up to thebytes-lengthbytes)skip-kth character (whenerr-charis#f), or if the substring decoding produces fewer thanskip-kcharacters, the result is#f. Iferr-charis not#f, it is used to resolve decoding errors as inbytes->string/utf-8.(bytes-utf-8-indexbytes[skip-k err-char start-k end-k])returns the offset in bytes intobytesat which theskip-kth character's encoding starts in the UTF-8 decoding ofbytes's substring fromstart-ktoend-k(but without actually generating the other decoded characters). Ifstart-kis not provided, it defaults to0, andend-kdefaults to(. The result is relative to the start ofbytes-lengthbytes)bytes, not tostart-k. If the substring is not a UTF-8 encoding up to theskip-kth character (whenerr-charis#f), or if the substring decoding produces fewer thanskip-kcharacters, the result is#f. Iferr-charis not#f, it is used to resolve decoding errors as inbytes->string/utf-8.
A string converter can be used to convert directly from one byte-string encoding of characters to another byte-string encoding.
(bytes-open-converterfrom-name-string to-name-string)-- produces a string converter to go from the encoding named byfrom-name-stringto the encoding named byto-name-string. If the requested conversion pair is not available,#fis returned instead of a converter.Certain encoding combinations are always available:
(-- the identity conversion, except that encoding errors in the input lead to a decoding failure.bytes-open-converter"UTF-8" "UTF-8")(-- the identity conversion, except that any input byte that is not part of a valid encoding sequence is effectively replaced bybytes-open-converter"UTF-8-permissive" "UTF-8")(. (This handling of invalid sequences is consistent with the interpretation of port bytes streams into characters; see section 11.1.)char->integer#\?)(-- converts from the current locale's default encoding (see section 1.2.2) to UTF-8.bytes-open-converter"" "UTF-8")(-- converts from UTF-8 to the current locale's default encoding (see section 1.2.2).bytes-open-converter"UTF-8" "")(-- converts UTF-8 to UTF-16 under Unix and Mac OS X, where each UTF-16 code unit is a sequence of two bytes ordered by the current platform's endianess. Under Windows, the input can include encodings that are not valid UTF-8, but which naturally extend the UTF-8 encoding to support unpaired surrogate code units, and the output is a sequence of UTF-16 code units (as little-endian byte pairs), potentially including unpaired surrogates.bytes-open-converter"platform-UTF-8" "platform-UTF-16")(-- likebytes-open-converter"platform-UTF-8-permissive" "platform-UTF-16")(, but an input byte that is not part of a valid UTF-8 encoding sequence (or valid for the unpaired-surrogate extension under Windows) is effectively replaced withbytes-open-converter"platform-UTF-8" "platform-UTF-16")(.char->integer#\?)(-- converts UTF-16 (bytes orderd by the current platform's endianness) to UTF-8 under Unix and Mac OS X. Under Windows, the input can include UTF-16 code units that are unpaired surrogates, and the corresponding output includes an encoding of each surrogate in a natural extension of UTF-8. Under Unix and Mac OS X, surrogates are assumed to be paired: a pair of bytes with the bitsbytes-open-converter"platform-UTF-16" "platform-UTF-8")#xD800starts a surrogate pair, and the#x03FFbits are used from the pair and following pair (independent of the value of the#xDC00bits). On all platforms, performance may be poor when decoding from an odd offset within an input byte string.
A newly opened byte converter is registered with the current custodian (see section 9.2), so that the converter is closed when the custodian is shut down. A converter is not registered with a custodian (and does not need to be closed) if it is one of the guaranteed combinations not involving
""under Unix, or if it is any of the guaranteed combinations (including"") under Windows and Mac OS X.The set of available encodings and combinations varies by platform, depending on the iconv library that is installed. Under Windows, iconv.dll or libiconv.dll must be in the same directory as libmzschVERS.dll (where VERS is a version number),10 in the user's path, in the system directory, or in the current executable's directory at run time, and the DLL must either supply _errno or link to msvcrt.dll for _errno; otherwise, only the guaranteed combinations are available.
(bytes-close-converterbytes-converter)-- closes the given converter, so that it can no longer be used withbytes-convertorbytes-convert-end.(bytes-convertbytes-converter src-bytes[src-start-k src-end-k dest-bytes dest-start-k dest-end-k])converts the bytes fromsrc-start-ktosrc-end-kinsrc-bytes. Ifdest-bytesis supplied and not#f, the converted byte are written intodest-bytesfromdest-start-ktodest-end-k. Ifdest-bytesis not supplied or is#f, then a newly allocated byte string holds the conversion results, and the size of the result byte string is no more than(- dest-end-k start-start-k).If
src-start-kordest-start-kis not provided, it defaults to0. Ifsrc-end-kis not provided, it defaults to(. Ifbytes-lengthsrc-bytessrc-end-kis not provided or is#f, then it defaults to(whenbytes-lengthdest-bytes)dest-bytesis a byte string or to an arbitrarily large integer otherwise.The result of
bytes-convertis three values:result-bytesordest-wrote-k-- a byte string ifdest-bytesis#for not provided, or the number of bytes written intodest-bytesotherwise.src-read-k-- the number of bytes successfully converted fromsrc-bytes.'complete,'continues,'aborts, or'error-- indicates how conversion terminated.'complete: The entire input was processed, andsrc-read-kwill be equal to(- src-end-k src-start-k).'continues: Conversion stopped due to the limit on the result size or the space indest-bytes; in this case, fewer than(- dest-end-k dest-start-k)bytes may be returned if more space is needed to process the next complete encoding sequence insrc-bytes.'aborts: The input stopped part-way through an encoding sequence, and more input bytes are necessary to continue. For example, if the last byte of input is#o303for a"UTF-8-permissive"decoding, the result is'aborts, because another byte is needed to determine how to use the#o303byte.'error: The bytes starting at(+ src-start-k src-read-k)bytes insrc-bytesdo not form a legal encoding sequence. This result is never produced for some encodings, where all byte sequences are valid encodings. For example, since"UTF-8-permissive"handles an invalid UTF-8 sequence by dropping characters or generating ``?'', every byte sequence is effectively valid.
Applying a converter accumulates state in the converter (even when the third result of
bytes-convertis'complete). This state can affect both further processing of input and further generation of output, but only for conversions that involve ``shift sequences'' to change modes within a stream. To terminate an input sequence and reset the converter, usebytes-convert-end.(bytes-convert-endbytes-converter[dest-bytes dest-start-k dest-end-k])-- likebytes-convert, but instead of converting bytes, this procedure generates an ending sequence for the conversion (sometimes called a ``shift sequence''), if any. Few encodings use shift sequences, so this function will succeed with no output for most encodings. In any case, successful output of a (possibly empty) shift sequence resets the converter to its initial state.The result of
bytes-convert-endis two values:result-bytesordest-wrote-k-- a byte string ifdest-bytesis#for not provided, or the number of bytes written intodest-bytesotherwise.'completeor'continues-- indicates whether conversion completed. If'complete, then an entire ending sequence was produced. If'continues, then the conversion could not complete due to the limit on the result size or the space indest-bytes, and the first result is either an empty byte string or0.
(bytes-converter?v)returns#tifvis a byte converter produced bybytes-open-converter,#fotherwise.(locale-string-encoding)returns a string for the current locale's encoding (i.e., the encoding normally identified by""). See alsosystem-language+countryin section 15.5.
3.7 Symbols
For information about symbol parsing and printing, see section 11.2.4 and section 11.2.5, respectively.
MzScheme provides two ways of generating an uninterned
symbol, i.e., a symbol that is not , eq?, or
eqv? to any other symbol, although it may print the same
as another symbol:
equal?
(string->uninterned-symbolstring)is like(, but the resulting symbol is a new uninterned symbol. Callingstring->symbolstring)twice with the samestring->uninterned-symbolstringreturns two distinct symbols.(gensym[symbol/string])creates an uninterned symbol with an automatically-generated name. The optionalsymbol/stringargument is a prefix symbol or string.
Regular (interned) symbols are only weakly held by the internal symbol
table. This weakness can never affect the result of
an , eq?, or eqv? test, but a symbol
may disappear when placed into a weak box (see section 13.1) used
as the key in a weak hash table (see section 3.14), or used as
an ephemeron key (see section 13.2).equal?
3.8 Keywords
A symbol-like datum that starts with a hash and colon (``#:'') is
parsed as a keyword constant. Keywords behave like
symbols -- two keywords are if and only if they print
the same -- but they are a distinct set of values.eq?
(keyword?v)returns#tifvis a keyword,#fotherwise.(keyword->stringkeyword)returns a string for theed form ofdisplaykeyword, not including the leading#:.(string->keywordstring)returns a keyword whoseed form is the same as that ofdisplaystring, but with a leading#:.
Like symbols, keywords are only weakly held by the internal keyword table; see section 3.7 for more information.
3.9 Vectors
When a vector is created with without a fill
value, it is initialized with make-vector0 in all positions. A vector
can be immutable, such as a vector returned by syntax-e, but
vectors generated by read are mutable. (See also
in section 3.10.)immutable?
(vector->immutable-vector vec) returns an immutable vector with
the same content as vec, and it returns vec itself if
vec is immutable. (See also in
section 3.10.)immutable?
(vector-immutable v ···1) is like (vector v ···1) except that
the resulting vector is immutable. (See also in
section 3.10.)immutable?
3.10 Lists
A cons cell can be mutable or immutable. When an immutable cons cell
is provided to a procedure like , the
set-cdr!exn:fail:contract exception is raised. Cons cells generated by are always mutable.read
The global variable null is bound to the empty list.
(reverse! list) is the same as (reverse , but
list)list is destructively reversed using (i.e.,
each cons cell in set-cdr!list is mutated).
(append! list ···1) is like
(append , but it destructively appends the
list)lists (i.e., except for the last list, the last cons cell
of each list is mutated to append the lists; empty lists are
essentially dropped).
(list* v ···1) is similar to (
but the last argument is used directly as the list v ···1) of the last
pair constructed for the list:
cdr
(list*1 2 3 4) ; =>'(1 2 3 . 4)
(cons-immutable v1 v2) returns an immutable pair whose
is carv1 and is cdrv2.
(list-immutable v ···1) is like (, but using
immutable pairs.list v ···1)
(list*-immutable v ···1) is like (, but using
immutable pairs.list* v ···1)
(immutable? v) returns #t if v is an immutable
cons cell, string, vector, box, or hash table, #f otherwise.
The list-ref and list-tail procedures accept an
improper list as a first argument. If either procedure is applied to
an improper list and an index that would require taking the
or car of a non-cons-cell, the
cdrexn:fail:contract exception is raised.
The member, memv, and memq procedures
accept an improper list as a second argument. If the membership
search reaches the improper tail, the
exn:fail:contract exception is raised.
The assoc, assv, and assq procedures
accept an improperly formed association list as a second argument.
If the association search reaches an improper list tail or a list
element that is not a pair, the exn:fail:contract exception is raised.
3.11 Boxes
MzScheme provides boxes, which are records that have a single field:
(boxv)returns a new mutable box that containsv.(box-immutablev)returns a new immutable box that containsv.(unboxbox)returns the content ofbox. For anyv,(unbox (boxreturnsv))v.(set-box!mutable-box v)sets the content ofmutable-boxtov.(box?v)returns#tifvis a box,#fotherwise.
Two boxes are equal? if the contents of the boxes are
.equal?
A box returned by (see section 12.2.2) is
immutable; if syntax-e is applied to such a box, the
set-box!exn:fail:contract exception is raised. A box produced by read (via
#&) is mutable. (See also in
section 3.10.)immutable?
3.12 Procedures
See section 4.6 for information on defining new procedure types.
3.12.1 Arity
MzScheme's procedure returns the input arity
of a procedure:
procedure-arity
(procedure-arityproc)returns information about the number of arguments accepted by the procedureproc. The resultais either:an exact non-negative integer ==> the procedure always takes exactly
aarguments;an
arity-at-least11 instance ==> the procedure takes(or more arguments; orarity-at-least-valuea)a list containing integers and
arity-at-leastinstances ==> the procedure takes any number of arguments that can match one of the arities in the list.
(procedure-arity-includes?proc k)returns#tif the procedure can acceptnarguments (wherekis an exact, non-negative integer),#fotherwise.
Examples:
(procedure-aritycons) ; =>2(procedure-aritylist) ; =>#<struct:arity-at-least>(arity-at-least?(procedure-aritylist)) ; =>#t(arity-at-least-value(procedure-aritylist)) ; =>0(arity-at-least-value(procedure-arity(lambda (x . y) x))) ; =>1(procedure-arity(case-lambda [(x) 0] [(x y) 1])) ; =>'(1 2)(procedure-arity-includes?cons2) ; =>#t(procedure-arity-includes?display3) ; =>#f
When compiling a lambda or case-lambda expression,
MzScheme looks for a 'method-arity-error property
attached to the expression (see section 12.6.2). If it is present
with a true value, and if no case of the procedure accepts zero
arguments, then the procedure is marked so that an
exn:fail:contract:arity exception involving the procedure
will hide the first argument, if one was provided. (Hiding the first
argument is useful when the procedure implements a method, where the
first argument is implicit in the original source). The property
affects only the format of exn:fail:contract:arity exceptions,
not the result of .procedure-arity
3.12.2 Primitives
A primitive procedure is a built-in procedure that is implemented in low-level language. Not all built-in procedures are primitives, but almost all R5RS procedures are primitives, as are most of the procedures described in this manual.
(primitive?v)returns#tifvis a primitive procedure or#fotherwise.(primitive-result-arityprim-proc)returns the arity of the result of the primitive procedureprim-proc(as opposed to the procedure's input arity as returned byarity; see section 3.12.1). For most primitives, this procedure returns1, since most primitives return a single value when applied. For information about arity values, see section 3.12.1.(primitive-closure?v)returns#tifvis internally implemented as a primitive closure rather than a simple primitive procedure,#fotherwise. This information is intended for use by the mzc compiler.
3.12.3 Procedure Names
See section 6.2.4 for information about the names of primitives,
and the names inferred for lambda and case-lambda
procedures.
3.12.4 Closure Equality
(procedure-closure-contents-eq? proc1, proc2) return #t
if the procedures proc1 and proc2 refer to the same code
closed over the same values, where each value is compared
with eq?.
Inlining and other compiler optimizations limit the usefulness of this
procedure, because code can be duplicated or merged. Since the amount
of duplication from inlining is limited,
however, is useful for
some caching purposes.procedure-closure-contents-eq?
Example:
(let ([f #f]) ;; Usingset!likely prevents inlining: (set! f (lambda (x) (lambda () x))) (procedure-closure-contents-eq?(f 'a) (f 'a)) ; =>#t, probably (procedure-closure-contents-eq?(f 'a) (f 'b))) ; =>#f, definitely (let ([f (lambda (x) (lambda () x))]) (procedure-closure-contents-eq?(f 'a) (f 'a))) ;; =>#f, probably, because inling likely duplicatesf's body
3.13 Promises
The force procedure can only be applied to values returned
by delay, and promises are never implicitly d.force
(promise? v) returns #t if v is a promise
created by delay, #f otherwise.
3.14 Hash Tables
(make-hash-table [flag-symbol flag-symbol]) creates and returns a
new hash table. If provided, each flag-symbol must one of
the following:
'weak-- creates a hash table with weakly-held keys (see section 13.1).'equal-- creates a hash table that compares keys usinginstead ofequal?(needed, for example, when using strings as keys).eq?
By default, key comparisons use . If the second
eq?flag-symbol is redundant, the
exn:fail:contract exception is raised.
Two hash tables are if they are created with the same
flags, and if they map the same keys to equal? values (where
``same key'' means either equal? or eq?, depending
on the way the hash table compares keys).equal?
(make-immutable-hash-table assoc-list [flag-symbol]) creates an
immutable hash table. (See also in
section 3.10.) The immutable?assoc-list must be a list of pairs, where
the of each pair is a key, and the car is the
corresponding value. The mappings are added to the table in the order
that they appear in cdrassoc-list, so later mappings can hide
earlier mappings. If the optional flag-symbol argument is
provided, it must be 'equal, and the created hash table
compares keys with ; otherwise, the created table
compares keys with equal?.eq?
(hash-table? v [flag-symbol flag-symbol]) returns #t if
v was created by or
make-hash-tablemake-immutable-hash-table with the given flag-symbols
(or more), #f otherwise. Each provided flag-symbol
must be a distinct flag supported by make-hash-table; if the
second flag-symbol is redundant, the
exn:fail:contract exception is raised.
(hash-table-put! hash-table key-v v) maps key-v to v
in hash-table, overwriting any existing mapping for
key-v. If hash-table is immutable, the
exn:fail:contract exception is raised.
(hash-table-get hash-table key-v [failure-thunk-or-value])
returns the value for key-v in hash-table. If no value is
found for key-v, then failure-thunk-or-value determines
the result: if failure-thunk-or-value is not provided, the
exn:fail:contract exception is raised; if failure-thunk-or-value is a
procedure, it is called (through a tail call) with no
arguments to produce the result; finally, if
failure-thunk-or-value is provided and not a procedure, it is
used as the result.
(hash-table-remove! hash-table key-v) removes the value mapping
for key-v if it exists in hash-table. If
hash-table is immutable, the exn:fail:contract exception is raised.
(hash-table-map hash-table proc) applies the procedure proc
to each element in hash-table, accumulating the results into a
list. The procedure proc must take two arguments: a key and its
value. See the caveat below about concurrent modification.
(hash-table-for-each hash-table proc) applies the procedure
proc to each element in hash-table (for the side-effects
of proc) and returns void. The procedure proc must
take two arguments: a key and its value. See the caveat below about
concurrent modification.
(hash-table-count hash-table) returns the number of keys mapped
by hash-table. If hash-table is not created with
'weak, then the result is computed in constant time and
atomically. If hash-table is created with 'weak, see
the caveat below about concurrent modification.
(hash-table-copy hash-table) returns a mutable hash table with
the same mappings, same key-comparison mode, and same key-holding
strength as hash-table.
(eq-hash-code v) returns an exact integer; for any two
values, the returned integer is the same. Furthermore,
for the result integer eq?k and any other exact integer j,
(= k j) implies (.eq? k j)
(equal-hash-code v) returns an exact integer; for any two
values, the returned integer is the same.
Furthermore, for the result integer equal?k and any other exact
integer j, (= k j) implies (. If
eq? k j)v contains a cycle through pairs, vectors, boxes, and
inspectable structure fields, then equal-hash-code applied
to v will loop indefinitely.
Caveat concerning concurrent modification: A hash table can be
manipulated with , hash-table-get,
and hash-table-put! concurrently by multiple threads,
and the operations are protected by a table-specific semaphore as
needed. A few caveats apply, however:
hash-table-remove!
If a thread is terminated while applying
,hash-table-get, orhash-table-put!to a hash table that useshash-table-remove!comparisons, all current and future operations on the hash table block indefinitely.equal?The
,hash-table-map, andhash-table-for-eachprocedures do not use the table's semaphore. Consequently, if a hash table is extended with new keys by another thread while a map, for-each, or count is in process, arbitrary key-value pairs can be dropped or duplicated in the map or for-each. Similarly, if a map or for-each procedure itself extends the table, arbitrary key-value pairs can be dropped or duplicated. However, key mappings can be deleted or remapped by any thread with no adverse affects (i.e., the change does not affect a traversal if the key has been seen already, otherwise the traversal skips a deleted key or uses the remapped key's new value).hash-table-count
Caveat concerning mutable keys: If a key into an
-based hash table is mutated (e.g., a key string is
modified with equal?), then the hash table's behavior
for put and get operations becomes unpredictable.string-set!
4 30 bits for a 32-bit architecture, 62 bits for a 64-bit architecture.
5 This definition of
technically contradicts R5RS, but R5RS does not address
strange ``numbers'' like eqv?+nan.0.
6 The random number generator uses a 54-bit version of L'Ecuyer's MRG32k3a algorithm.
7 The current version of MzScheme uses Unicode version 4.1.
8 See also the Latin-1 footnote of section 1.2.3.
9 See also the Latin-1 footnote of section 1.2.3.
10 In PLT's software distributions for Windows, a suitable iconv.dll is included with libmzschVERS.dll.
11 The
arity-at-least structure type is transparent to all
inspectors (see section 4.5).