forked from cisco/ChezScheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.stex
3163 lines (2613 loc) · 112 KB
/
objects.stex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% Copyright 2005-2016 Cisco Systems, Inc.
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
\chapter{Operations on Objects\label{CHPTOBJECTS}}
This chapter describes operations specific to {\ChezScheme} on
nonnumeric objects, including standard objects such as pairs and
numbers and {\ChezScheme} extensions such as boxes and records.
Chapter~\ref{CHPTNUMERIC} describes operations on numbers.
See Chapter~\ref{TSPL:CHPTOBJECTS} of {\TSPLFOUR} or the Revised$^6$ Report
on Scheme for a description of standard operations on objects.
\section{Missing R6RS Type Predicates\label{SECTMISSINGR6RSTYPEPREDS}}
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{enum-set?}{\categoryprocedure}{(enum-set? \var{obj})}
\returns \scheme{#t} if \var{obj} is an enum set, \scheme{#f} otherwise
\listlibraries
\endnoskipentryheader
This predicate is not defined by the Revised$^6$ Report, but should be.
%----------------------------------------------------------------------------
\entryheader
\formdef{record-constructor-descriptor?}{\categoryprocedure}{(record-constructor-descriptor? \var{obj})}
\returns \scheme{#t} if \var{obj} is a record constructor descriptor, \scheme{#f} otherwise
\listlibraries
\endentryheader
This predicate is not defined by the Revised$^6$ Report, but should be.
\section{Pairs and Lists}
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{atom?}{\categoryprocedure}{(atom? \var{obj})}
\returns \scheme{#t} if \var{obj} is not a pair, \scheme{#f} otherwise
\listlibraries
\endnoskipentryheader
\noindent
\scheme{atom?} is equivalent to \scheme{(lambda (x) (not (pair? x)))}.
\schemedisplay
(atom? '(a b c)) ;=> #f
(atom? '(3 . 4)) ;=> #f
(atom? '()) ;=> #t
(atom? 3) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{list-head}{\categoryprocedure}{(list-head \var{list} \var{n})}
\returns a list of the first \var{n} elements of \var{list}
\listlibraries
\endentryheader
\noindent
\var{n} must be an exact nonnegative integer less than or equal to
the length of \var{list}.
\scheme{list-head} and the standard Scheme procedure \scheme{list-tail}
may be used together to split a list into two separate lists.
While \scheme{list-tail} performs no allocation but instead returns a
sublist of the original list, \scheme{list-head} always returns a copy
of the first portion of the list.
\scheme{list-head} may be defined as follows.
\schemedisplay
(define list-head
(lambda (ls n)
(if (= n 0)
'()
(cons (car ls) (list-head (cdr ls) (- n 1))))))
(list-head '(a b c) 0) ;=> ()
(list-head '(a b c) 2) ;=> (a b)
(list-head '(a b c) 3) ;=> (a b c)
(list-head '(a b c . d) 2) ;=> (a b)
(list-head '(a b c . d) 3) ;=> (a b c)
(list-head '#1=(a . #1#) 5) ;=> (a a a a a)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{last-pair}{\categoryprocedure}{(last-pair \var{list})}
\returns the last pair of a \var{list}
\listlibraries
\endentryheader
\noindent
\var{list} must not be empty.
\scheme{last-pair} returns the last pair (not the last element) of \var{list}.
\var{list} may be an improper list, in which case the last pair is the
pair containing the last element and the terminating object.
\schemedisplay
(last-pair '(a b c d)) ;=> (d)
(last-pair '(a b c . d)) ;=> (c . d)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{list-copy}{\categoryprocedure}{(list-copy \var{list})}
\returns a copy of \var{list}
\listlibraries
\endentryheader
\noindent
\scheme{list-copy} returns a list \scheme{equal?} to \var{list}, using new pairs
to reform the top-level list structure.
\schemedisplay
(list-copy '(a b c)) ;=> (a b c)
(let ([ls '(a b c)])
(equal? ls (list-copy ls))) ;=> #t
(let ([ls '(a b c)])
(let ([ls-copy (list-copy ls)])
(or (eq? ls-copy ls)
(eq? (cdr ls-copy) (cdr ls))
(eq? (cddr ls-copy) (cddr ls))))) ;=> #f
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{list*}{\categoryprocedure}{(list* \var{obj} \dots \var{final-obj})}
\returns a list of \scheme{\var{obj} \dots} terminated by \var{final-obj}
\listlibraries
\endentryheader
\noindent
\scheme{list*} is identical to the Revised$^6$ Report \scheme{cons*}.
%----------------------------------------------------------------------------
\entryheader
\formdef{make-list}{\categoryprocedure}{(make-list \var{n})}
\formdef{make-list}{\categoryprocedure}{(make-list \var{n} \var{obj})}
\returns a list of \var{n} \var{objs}
\listlibraries
\endentryheader
\noindent
\var{n} must be a nonnegative integer.
If \var{obj} is omitted, the elements of the list are unspecified.
\schemedisplay
(make-list 0 '()) ;=> ()
(make-list 3 0) ;=> (0 0 0)
(make-list 2 "hi") ;=> ("hi" "hi")
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{iota}{\categoryprocedure}{(iota \var{n})}
\returns a list of integers from 0 (inclusive) to \var{n} (exclusive)
\listlibraries
\endentryheader
\var{n} must be an exact nonnegative integer.
\schemedisplay
(iota 0) ;=> ()
(iota 5) ;=> (0 1 2 3 4)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{enumerate}{\categoryprocedure}{(enumerate \var{ls})}
\returns a list of integers from 0 (inclusive) to the length of \var{ls} (exclusive)
\listlibraries
\endentryheader
\schemedisplay
(enumerate '()) ;=> ()
(enumerate '(a b c)) ;=> (0 1 2)
(let ([ls '(a b c)])
(map cons ls (enumerate ls))) ;=> ((a . 0) (b . 1) (c . 2))
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{remq!}{\categoryprocedure}{(remq! \var{obj} \var{list})}
\formdef{remv!}{\categoryprocedure}{(remv! \var{obj} \var{list})}
\formdef{remove!}{\categoryprocedure}{(remove! \var{obj} \var{list})}
\returns a list containing the elements of \var{list} with all occurrences of \var{obj} removed
\listlibraries
\endentryheader
\noindent
These procedures are similar to the Revised$^6$ Report
\scheme{remq}, \scheme{remv}, and \scheme{remove} procedures, except
\scheme{remq!}, \scheme{remv!} and \scheme{remove!} use pairs from the
input list to build the output list.
They perform less allocation but are not
necessarily faster than their nondestructive counterparts.
Their use can easily lead to confusing or incorrect results if used
indiscriminately.
\schemedisplay
(remq! 'a '(a b a c a d)) ;=> (b c d)
(remv! #\a '(#\a #\b #\c)) ;=> (#\b #\c)
(remove! '(c) '((a) (b) (c))) ;=> ((a) (b))
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{substq}{\categoryprocedure}{(substq \var{new} \var{old} \var{tree})}
\formdef{substv}{\categoryprocedure}{(substv \var{new} \var{old} \var{tree})}
\formdef{subst}{\categoryprocedure}{(subst \var{new} \var{old} \var{tree})}
\formdef{substq!}{\categoryprocedure}{(substq! \var{new} \var{old} \var{tree})}
\formdef{substv!}{\categoryprocedure}{(substv! \var{new} \var{old} \var{tree})}
\formdef{subst!}{\categoryprocedure}{(subst! \var{new} \var{old} \var{tree})}
\returns a tree with \var{new} substituted for occurrences of \var{old} in \var{tree}
\listlibraries
\endentryheader
\noindent
These procedures traverse \var{tree}, replacing all objects equivalent to
the object \var{old} with the object \var{new}.
The equivalence test for \scheme{substq} and \scheme{substq!} is \scheme{eq?},
for \scheme{substv} and \scheme{substv!} is \scheme{eqv?},
and for \scheme{subst} and \scheme{subst!} is \scheme{equal?}.
\scheme{substq!}, \scheme{substv!}, and \scheme{subst!} perform the
substitutions destructively.
They perform less allocation but are not
necessarily faster than their nondestructive counterparts.
Their use can easily lead to confusing or incorrect results if used
indiscriminately.
\schemedisplay
(substq 'a 'b '((b c) b a)) ;=> ((a c) a a)
(substv 2 1 '((1 . 2) (1 . 4) . 1)) ;=> ((2 . 2) (2 . 4) . 2)
(subst 'a
'(a . b)
'((a . b) (c a . b) . c)) ;=> (a (c . a) . c)
(let ([tr '((b c) b a)])
(substq! 'a 'b tr)
tr) ;=> ((a c) a a)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{reverse!}{\categoryprocedure}{(reverse! \var{list})}
\returns a list containing the elements of \var{list} in reverse order
\listlibraries
\endentryheader
\noindent
\scheme{reverse!} destructively reverses \var{list}
by reversing its links.
Using \scheme{reverse!} in place of \scheme{reverse} reduces allocation but is not
necessarily faster than \scheme{reverse}.
Its use can easily lead to confusing or incorrect results if used
indiscriminately.
\schemedisplay
(reverse! '()) ;=> ()
(reverse! '(a b c)) ;=> (c b a)
(let ([x '(a b c)])
(reverse! x)
x) ;=> (a)
(let ([x '(a b c)])
(set! x (reverse! x))
x) ;=> (c b a)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{append!}{\categoryprocedure}{(append! \var{list} \dots)}
\returns the concatenation of the input lists
\listlibraries
\endentryheader
\noindent
Like \scheme{append},
\scheme{append!} returns a new list consisting of the elements of the first
list followed by the elements of the second list, the elements of the
third list, and so on.
Unlike \scheme{append},
\scheme{append!} reuses the pairs in all of the
arguments in forming the new list.
That is, the last cdr of each list argument but the last is changed to
point to the next list argument.
If any argument but the last is the empty list, it is essentially ignored.
The final argument (which need not be a list) is not altered.
\scheme{append!} performs less allocation than \scheme{append} but is not
necessarily faster.
Its use can easily lead to confusing or incorrect results if used
indiscriminately.
\schemedisplay
(append! '(a b) '(c d)) ;=> (a b c d)
(let ([x '(a b)])
(append! x '(c d))
x) ;=> (a b c d)
\endschemedisplay
\section{Characters}
{\ChezScheme} extends the syntax of characters in two ways.
First, a \scheme{#\} prefix followed by exactly three octal digits is read
as a character whose numeric code is the octal value of the three digits,
e.g., \scheme{#\044} is read as \scheme{#\$}.
Second, it recognizes several nonstandard named characters:
\scheme{#\rubout} (which is the same as \scheme{#\delete}),
\scheme{#\bel} (which is the same as \scheme{#\alarm}),
\scheme{#\vt} (which is the same as \scheme{#\vtab}),
\scheme{#\nel} (the Unicode NEL character), and
\scheme{#\ls} (the Unicode LS character).
The set of nonstandard character names may be changed via the procedure
\index{\scheme{char-name}}\scheme{char-name} (page \ref{desc:char-name}).
These extensions are disabled in an input stream after \scheme{#!r6rs} has
been seen by the reader, unless \scheme{#!chezscheme} has been seen more
recently.
%----------------------------------------------------------------------------
\entryheader
\formdef{char=?}{\categoryprocedure}{(char=? \var{char_1} \var{char_2} \dots)}
\formdef{char<?}{\categoryprocedure}{(char<? \var{char_1} \var{char_2} \dots)}
\formdef{char>?}{\categoryprocedure}{(char>? \var{char_1} \var{char_2} \dots)}
\formdef{char<=?}{\categoryprocedure}{(char<=? \var{char_1} \var{char_2} \dots)}
\formdef{char>=?}{\categoryprocedure}{(char>=? \var{char_1} \var{char_2} \dots)}
\formdef{char-ci=?}{\categoryprocedure}{(char-ci=? \var{char_1} \var{char_2} \dots)}
\formdef{char-ci<?}{\categoryprocedure}{(char-ci<? \var{char_1} \var{char_2} \dots)}
\formdef{char-ci>?}{\categoryprocedure}{(char-ci>? \var{char_1} \var{char_2} \dots)}
\formdef{char-ci<=?}{\categoryprocedure}{(char-ci<=? \var{char_1} \var{char_2} \dots)}
\formdef{char-ci>=?}{\categoryprocedure}{(char-ci>=? \var{char_1} \var{char_2} \dots)}
\returns \scheme{#t} if the relation holds, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noindent
These predicates are identical to the Revised$^6$ Report counterparts,
except they are extended to accept one or more rather than two or more
arguments.
When passed one argument, each of these predicates returns \scheme{#t}.
\schemedisplay
(char>? #\a) ;=> #t
(char<? #\a) ;=> #t
(char-ci=? #\a) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{char-}{\categoryprocedure}{(char- \var{char_1} \var{char_2})}
\returns the integer difference between \var{char_1} and \var{char_2}
\listlibraries
\endentryheader
\noindent
\scheme{char-} subtracts the integer value of \var{char_2} from the
integer value of \var{char_1} and returns the difference.
The following examples assume that the integer representation is the
ASCII code for the character.
\schemedisplay
(char- #\f #\e) ;=> 1
(define digit-value
; returns the digit value of the base-r digit c, or #f if c
; is not a valid digit
(lambda (c r)
(let ([v (cond
[(char<=? #\0 c #\9) (char- c #\0)]
[(char<=? #\A c #\Z) (char- c #\7)]
[(char<=? #\a c #\z) (char- c #\W)]
[else 36])])
(and (fx< v r) v))))
(digit-value #\8 10) ;=> 8
(digit-value #\z 10) ;=> #f
(digit-value #\z 36) ;=> 35
\endschemedisplay
\noindent
\scheme{char-} might be defined as follows.
\schemedisplay
(define char-
(lambda (c1 c2)
(- (char->integer c1) (char->integer c2))))
\endschemedisplay
\section{Strings}
{\ChezScheme} extends the standard string syntax with two character
escapes: \scheme{\'}, which produces the single quote character, and
\scheme{\\var{nnn}}, i.e., backslash followed by 3 octal digits,
which produces the character equivalent of the octal value of
the 3 digits.
These extensions are disabled in an input stream after \scheme{#!r6rs} has
been seen by the reader, unless \scheme{#!chezscheme} has been seen more
recently.
The length and indices of a string in {\ChezScheme} are always fixnums.
%----------------------------------------------------------------------------
\entryheader
\formdef{string=?}{\categoryprocedure}{(string=? \var{string_1} \var{string_2} \var{string_3} \dots)}
\formdef{string<?}{\categoryprocedure}{(string<? \var{string_1} \var{string_2} \var{string_3} \dots)}
\formdef{string>?}{\categoryprocedure}{(string>? \var{string_1} \var{string_2} \var{string_3} \dots)}
\formdef{string<=?}{\categoryprocedure}{(string<=? \var{string_1} \var{string_2} \var{string_3} \dots)}
\formdef{string>=?}{\categoryprocedure}{(string>=? \var{string_1} \var{string_2} \var{string_3} \dots)}
\formdef{string-ci=?}{\categoryprocedure}{(string-ci=? \var{string_1} \var{string_2} \var{string_3} \dots)}
\formdef{string-ci<?}{\categoryprocedure}{(string-ci<? \var{string_1} \var{string_2} \var{string_3} \dots)}
\formdef{string-ci>?}{\categoryprocedure}{(string-ci>? \var{string_1} \var{string_2} \var{string_3} \dots)}
\formdef{string-ci<=?}{\categoryprocedure}{(string-ci<=? \var{string_1} \var{string_2} \var{string_3} \dots)}
\formdef{string-ci>=?}{\categoryprocedure}{(string-ci>=? \var{string_1} \var{string_2} \var{string_3} \dots)}
\returns \scheme{#t} if the relation holds, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noindent
These predicates are identical to the Revised$^6$ Report counterparts,
except they are extended to accept one or more rather than two or more
arguments.
When passed one argument, each of these predicates returns \scheme{#t}.
\schemedisplay
(string>? "a") ;=> #t
(string<? "a") ;=> #t
(string-ci=? "a") ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{string-copy!}{\categoryprocedure}{(string-copy! \var{src} \var{src-start} \var{dst} \var{dst-start} \var{n})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{src} and \var{dst} must be strings.
\var{src-start}, \var{dst-start}, and \var{n} must be exact nonnegative
integers.
The sum of \var{src-start} and \var{n} must not exceed the length of \var{src},
and the sum of \var{dst-start} and \var{n} must not exceed the length of \var{dst}.
\scheme{string-copy!} overwrites the \var{n} bytes of \var{dst}
starting at \var{dst-start} with the \var{n} bytes of \var{dst}
starting at \var{src-start}.
This works even if \var{dst} is the same string as \var{src} and the
source and destination locations overlap.
That is, the destination is filled with the characters that appeared at the
source before the operation began.
\schemedisplay
(define s1 "to boldly go")
(define s2 (make-string 10 #\-))
(string-copy! s1 3 s2 1 3)
s2 ;=> "-bol------"
(string-copy! s1 7 s2 4 2)
s2 ;=> "-bolly----"
(string-copy! s2 2 s2 5 4)
s2 ;=> "-bollolly-"
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{substring-fill!}{\categoryprocedure}{(substring-fill! \var{string} \var{start} \var{end} \var{char})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
The characters of \var{string} from \var{start} (inclusive) to \var{end}
(exclusive) are set to \var{char}.
\var{start} and \var{end} must be nonnegative integers; \var{start}
must be strictly less than the length of \var{string}, while \var{end} may
be less than or equal to the length of \var{string}.
If $end\le start$, the string is left unchanged.
\schemedisplay
(let ([str (string-copy "a tpyo typo")])
(substring-fill! str 2 6 #\X)
str) ;=> "a XXXX typo"
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{string-truncate!}{\categoryprocedure}{(string-truncate! \var{string} \var{n})}
\returns \var{string} or the empty string
\listlibraries
\endentryheader
\noindent
\var{n} must be an exact nonnegative fixnum not greater than the length of
\var{string}.
If \var{n} is zero, \scheme{string-truncate!} returns the empty string.
Otherwise, \var{string-truncate!} destructively truncates \var{string} to
its first \var{n} characters and returns \var{string}.
\schemedisplay
(define s (make-string 7 #\$))
(string-truncate! s 0) ;=> ""
s ;=> "$$$$$$$"
(string-truncate! s 3) ;=> "$$$"
s ;=> "$$$"
\endschemedisplay
\section{Vectors}
{\ChezScheme} extends the syntax of vectors to allow the length of the
vector to be specified between the \scheme{#} and open parenthesis, e.g.,
\scheme{#3(a b c)}.
If fewer elements are supplied in the syntax than the specified length,
each element after the last printed element is the same as the last
printed element.
This extension is disabled in an input stream after \scheme{#!r6rs} has
been seen by the reader, unless \scheme{#!chezscheme} has been seen more
recently.
The length and indices of a vector in {\ChezScheme} are always fixnums.
%----------------------------------------------------------------------------
\entryheader
\formdef{vector-copy}{\categoryprocedure}{(vector-copy \var{vector})}
\returns a copy of \var{vector}
\listlibraries
\endentryheader
\noindent
\scheme{vector-copy} creates a new vector of the same length and contents
as \var{vector}.
The elements themselves are not copied.
\schemedisplay
(vector-copy '#(a b c)) ;=> #(a b c)
(let ([v '#(a b c)])
(eq? v (vector-copy v))) ;=> #f
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{vector-set-fixnum!}{\categoryprocedure}{(vector-set-fixnum! \var{vector} \var{n} \var{fixnum})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\scheme{vector-set-fixnum!} changes the \var{n}th element of \var{vector} to \var{fixnum}.
\var{n} must be an exact nonnegative integer strictly less than
the length of \var{vector}.
It is faster to store a fixnum than an arbitrary value,
since for arbitrary values, the system has to record potential assignments from older to
younger objects to support generational garbage collection.
Care must be taken to ensure that the argument is indeed a fixnum, however;
otherwise, the collector may not properly track the assignment.
The primitive performs a fixnum check on the argument except at
optimization level~3.
See also the description of fixnum-only vectors (fxvectors) below.
\schemedisplay
(let ([v (vector 1 2 3 4 5)])
(vector-set-fixnum! v 2 73)
v) ;=> #(1 2 73 4 5)
\endschemedisplay
\section{Fixnum-Only Vectors\label{SECTFXVECTORS}}
Fixnum-only vectors, or ``fxvectors,'' are like vectors but contain
only fixnums.
Fxvectors are written with the \scheme{#vfx} prefix in place of the
\scheme{#} prefix for vectors, e.g., \scheme{#vfx(1 2 3)} or
\scheme{#10vfx(2)}.
The fxvector syntax is disabled in an input stream after \scheme{#!r6rs}
has been seen by the reader, unless \scheme{#!chezscheme} has been seen
more recently.
The length and indices of an fxvector are always fixnums.
Updating an fxvector is generally less expensive than updating a vector,
since for vectors, the system records potential assignments from older to
younger objects to support generational garbage collection.
The storage management system also takes advantage of the fact that
fxvectors contain no pointers to place them in an area of memory that
does not have to be traced during collection.
See also \scheme{vector-set-fixnum!} above.
%----------------------------------------------------------------------------
\entryheader
\formdef{fxvector?}{\categoryprocedure}{(fxvector? \var{obj})}
\returns \scheme{#t} if \var{obj} is an fxvector, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noskip\schemedisplay
(fxvector? #vfx()) ;=> #t
(fxvector? #vfx(1 2 3)) ;=> #t
(fxvector? (fxvector 1 2 3)) ;=> #t
(fxvector? '#(a b c)) ;=> #f
(fxvector? '(a b c)) ;=> #f
(fxvector? "abc") ;=> #f
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxvector}{\categoryprocedure}{(fxvector \var{fixnum} \dots)}
\returns an fxvector of the fixnums \scheme{\var{fixnum} \dots}
\listlibraries
\endentryheader
\noskip\schemedisplay
(fxvector) ;=> #vfx()
(fxvector 1 3 5) ;=> #vfx(1 3 5)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{make-fxvector}{\categoryprocedure}{(make-fxvector \var{n})}
\formdef{make-fxvector}{\categoryprocedure}{(make-fxvector \var{n} \var{fixnum})}
\returns an fxvector of length \var{n}
\listlibraries
\endentryheader
\noindent
\var{n} must be a fixnum.
If \var{fixnum} is supplied, each element of the fxvector is initialized
to \var{fixnum}; otherwise, the elements are unspecified.
\schemedisplay
(make-fxvector 0) ;=> #vfx()
(make-fxvector 0 7) ;=> #vfx()
(make-fxvector 5 7) ;=> #vfx(7 7 7 7 7)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxvector-length}{\categoryprocedure}{(fxvector-length \var{fxvector})}
\returns the number of elements in \var{fxvector}
\listlibraries
\endentryheader
\schemedisplay
(fxvector-length #vfx()) ;=> 0
(fxvector-length #vfx(1 2 3)) ;=> 3
(fxvector-length #10vfx(1 2 3)) ;=> 10
(fxvector-length (fxvector 1 2 3 4)) ;=> 4
(fxvector-length (make-fxvector 300)) ;=> 300
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxvector-ref}{\categoryprocedure}{(fxvector-ref \var{fxvector} \var{n})}
\returns the \var{n}th element (zero-based) of \var{fxvector}
\listlibraries
\endentryheader
\noindent
\var{n} must be a nonnegative fixnum strictly less than
the length of \var{fxvector}.
\schemedisplay
(fxvector-ref #vfx(-1 2 4 7) 0) ;=> -1
(fxvector-ref #vfx(-1 2 4 7) 1) ;=> 2
(fxvector-ref #vfx(-1 2 4 7) 3) ;=> 7
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxvector-set!}{\categoryprocedure}{(fxvector-set! \var{fxvector} \var{n} \var{fixnum})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{n} must be a nonnegative fixnum strictly less than
the length of \var{fxvector}.
\scheme{fxvector-set!} changes the \var{n}th element of \var{fxvector} to \var{fixnum}.
\schemedisplay
(let ([v (fxvector 1 2 3 4 5)])
(fxvector-set! v 2 (fx- (fxvector-ref v 2)))
v) ;=> #vfx(1 2 -3 4 5)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxvector-fill!}{\categoryprocedure}{(fxvector-fill! \var{fxvector} \var{fixnum})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\scheme{fxvector-fill!} replaces each element of \var{fxvector} with \var{fixnum}.
\schemedisplay
(let ([v (fxvector 1 2 3)])
(fxvector-fill! v 0)
v) ;=> #vfx(0 0 0)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxvector->list}{\categoryprocedure}{(fxvector->list \var{fxvector})}
\returns a list of the elements of \var{fxvector}
\listlibraries
\endentryheader
\schemedisplay
(fxvector->list (fxvector)) ;=> ()
(fxvector->list #vfx(7 5 2)) ;=> (7 5 2)
(let ([v #vfx(1 2 3 4 5)])
(apply fx* (fxvector->list v))) ;=> 120
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{list->fxvector}{\categoryprocedure}{(list->fxvector \var{list})}
\returns an fxvector of the elements of \var{list}
\listlibraries
\endentryheader
\noindent
\var{list} must consist entirely of fixnums.
\schemedisplay
(list->fxvector '()) ;=> #vfx()
(list->fxvector '(3 5 7)) ;=> #vfx(3 5 7)
(let ([v #vfx(1 2 3 4 5)])
(let ([ls (fxvector->list v)])
(list->fxvector (map fx* ls ls)))) ;=> #vfx(1 4 9 16 25)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxvector-copy}{\categoryprocedure}{(fxvector-copy \var{fxvector})}
\returns a copy of \var{fxvector}
\listlibraries
\endnoskipentryheader
\noindent
\scheme{fxvector-copy} creates a new fxvector with the same length and contents
as \var{fxvector}.
\schemedisplay
(fxvector-copy #vfx(3 4 5)) ;=> #vfx(3 4 5)
(let ([v #vfx(3 4 5)])
(eq? v (fxvector-copy v))) ;=> #f
\endschemedisplay
\section{Bytevectors\label{SECTBYTEVECTORS}}
As with vectors, {\ChezScheme} extends the syntax of bytevectors to allow
the length of the vector to be specified between the \scheme{#} and open
parenthesis, e.g., \scheme{#3vu8(1 105 73)}.
If fewer elements are supplied in the syntax than the specified length,
each element after the last printed element is the same as the last
printed element.
This extension is disabled in an input stream after \scheme{#!r6rs} has
been seen by the reader, unless \scheme{#!chezscheme} has been seen more
recently.
{\ChezScheme} also extends the set of bytevector primitives, including
primitives for loading and storing 3, 5, 6, and 7-byte quantities.
The length and indices of a bytevector in {\ChezScheme} are always fixnums.
%----------------------------------------------------------------------------
\entryheader
\formdef{bytevector}{\categoryprocedure}{(bytevector \var{fill} \dots)}
\returns a new bytevector containing \scheme{\var{fill} \dots}
\listlibraries
\endentryheader
Each \var{fill} value must be an exact integer representing a signed or
unsigned 8-bit value, i.e.,
a value in the range -128 to 255 inclusive.
A negative fill value is treated as its two's complement equivalent.
\schemedisplay
(bytevector) ;=> #vu8()
(bytevector 1 3 5) ;=> #vu8(1 3 5)
(bytevector -1 -3 -5) ;=> #vu8(255 253 251)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{bytevector->s8-list}{\categoryprocedure}{(bytevector->s8-list \var{bytevector})}
\returns a new list of the 8-bit signed elements of \var{bytevector}
\listlibraries
\endentryheader
The values in the returned list are exact eight-bit signed integers,
i.e., values in the range -128 to 127 inclusive.
\scheme{bytevector->s8-list} is similar to the Revised$^6$ Report
\scheme{bytevector->u8-list} except the values in the returned list
are signed rather than unsigned.
\schemedisplay
(bytevector->s8-list (make-bytevector 0)) ;=> ()
(bytevector->s8-list #vu8(1 127 128 255)) ;=> (1 127 -128 -1)
(let ([v #vu8(1 2 3 255)])
(apply * (bytevector->s8-list v))) ;=> -6
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{s8-list->bytevector}{\categoryprocedure}{(s8-list->bytevector \var{list})}
\returns a new bytevector of the elements of \var{list}
\listlibraries
\endentryheader
\var{list} must consist entirely of exact eight-bit signed integers, i.e.,
values in the range -128 to 127 inclusive.
\scheme{s8-list->bytevector} is similar to the Revised$^6$ Report
procedure
\scheme{u8-list->bytevector}, except the elements of the input list
are signed rather than unsigned.
\schemedisplay
(s8-list->bytevector '()) ;=> #vu8()
(s8-list->bytevector '(1 127 -128 -1)) ;=> #vu8(1 127 128 255)
(let ([v #vu8(1 2 3 4 5)])
(let ([ls (bytevector->s8-list v)])
(s8-list->bytevector (map - ls)))) ;=> #vu8(255 254 253 252 251)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{bytevector-truncate!}{\categoryprocedure}{(bytevector-truncate! \var{bytevector} \var{n})}
\returns \var{bytevector} or the empty bytevector
\listlibraries
\endentryheader
\noindent
\var{n} must be an exact nonnegative fixnum not greater than the length of
\var{bytevector}.
If \var{n} is zero, \scheme{bytevector-truncate!} returns the empty bytevector.
Otherwise, \var{bytevector-truncate!} destructively truncates \var{bytevector} to
its first \var{n} bytes and returns \var{bytevector}.
\schemedisplay
(define bv (make-bytevector 7 19))
(bytevector-truncate! bv 0) ;=> #vu8()
bv ;=> #vu8(19 19 19 19 19 19 19)
(bytevector-truncate! bv 3) ;=> #vu8(19 19 19)
bv ;=> #vu8(19 19 19)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{bytevector-u24-ref}{\categoryprocedure}{(bytevector-u24-ref \var{bytevector} \var{n} \var{eness})}
\returns the 24-bit unsigned integer at index \var{n} (zero-based) of \var{bytevector}
\formdef{bytevector-s24-ref}{\categoryprocedure}{(bytevector-s24-ref \var{bytevector} \var{n} \var{eness})}
\returns the 24-bit signed integer at index \var{n} (zero-based) of \var{bytevector}
\formdef{bytevector-u40-ref}{\categoryprocedure}{(bytevector-u40-ref \var{bytevector} \var{n} \var{eness})}
\returns the 40-bit unsigned integer at index \var{n} (zero-based) of \var{bytevector}
\formdef{bytevector-s40-ref}{\categoryprocedure}{(bytevector-s40-ref \var{bytevector} \var{n} \var{eness})}
\returns the 40-bit signed integer at index \var{n} (zero-based) of \var{bytevector}
\formdef{bytevector-u48-ref}{\categoryprocedure}{(bytevector-u48-ref \var{bytevector} \var{n} \var{eness})}
\returns the 48-bit unsigned integer at index \var{n} (zero-based) of \var{bytevector}
\formdef{bytevector-s48-ref}{\categoryprocedure}{(bytevector-s48-ref \var{bytevector} \var{n} \var{eness})}
\returns the 48-bit signed integer at index \var{n} (zero-based) of \var{bytevector}
\formdef{bytevector-u56-ref}{\categoryprocedure}{(bytevector-u56-ref \var{bytevector} \var{n} \var{eness})}
\returns the 56-bit unsigned integer at index \var{n} (zero-based) of \var{bytevector}
\formdef{bytevector-s56-ref}{\categoryprocedure}{(bytevector-s56-ref \var{bytevector} \var{n} \var{eness})}
\returns the 56-bit signed integer at index \var{n} (zero-based) of \var{bytevector}
\listlibraries
\endentryheader
\noindent
\var{n} must be an exact nonnegative integer and
indexes the starting byte of the value.
The sum of \var{n} and the number of bytes occupied by the value
(3 for 24-bit values, 5 for 40-bit values, 6 for 48-bit values,
and 7 for 56-bit values) must not exceed the length of \var{bytevector}.
\var{eness} must be a valid endianness symbol naming the endianness.
The return value is an exact integer in the appropriate range for
the number of bytes occupied by the value.
Signed values are the equivalent of the stored value treated as a two's
complement value.
%----------------------------------------------------------------------------
\entryheader
\formdef{bytevector-u24-set!}{\categoryprocedure}{(bytevector-u24-set! \var{bytevector} \var{n} \var{u24} \var{eness})}
\formdef{bytevector-s24-set!}{\categoryprocedure}{(bytevector-s24-set! \var{bytevector} \var{n} \var{s24} \var{eness})}
\formdef{bytevector-u40-set!}{\categoryprocedure}{(bytevector-u40-set! \var{bytevector} \var{n} \var{u40} \var{eness})}
\formdef{bytevector-s40-set!}{\categoryprocedure}{(bytevector-s40-set! \var{bytevector} \var{n} \var{s40} \var{eness})}
\formdef{bytevector-u48-set!}{\categoryprocedure}{(bytevector-u48-set! \var{bytevector} \var{n} \var{u48} \var{eness})}
\formdef{bytevector-s48-set!}{\categoryprocedure}{(bytevector-s48-set! \var{bytevector} \var{n} \var{s48} \var{eness})}
\formdef{bytevector-u56-set!}{\categoryprocedure}{(bytevector-u56-set! \var{bytevector} \var{n} \var{u56} \var{eness})}
\formdef{bytevector-s56-set!}{\categoryprocedure}{(bytevector-s56-set! \var{bytevector} \var{n} \var{s56} \var{eness})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{n} must be an exact nonnegative integer and
indexes the starting byte of the value.
The sum of \var{n} and the number of bytes occupied by the value must
not exceed the length of \var{bytevector}.
\var{u24} must be a 24-bit unsigned value, i.e., a value in the range
0 to $2^{24}-1$ inclusive;
\var{s24} must be a 24-bit signed value, i.e., a value in the range
$-2^{23}$ to $2^{23}-1$ inclusive;
\var{u40} must be a 40-bit unsigned value, i.e., a value in the range
0 to $2^{40}-1$ inclusive;
\var{s40} must be a 40-bit signed value, i.e., a value in the range
$-2^{39}$ to $2^{39}-1$ inclusive;
\var{u48} must be a 48-bit unsigned value, i.e., a value in the range
0 to $2^{48}-1$ inclusive;
\var{s48} must be a 48-bit signed value, i.e., a value in the range
$-2^{47}$ to $2^{47}-1$ inclusive;
\var{u56} must be a 56-bit unsigned value, i.e., a value in the range
0 to $2^{56}-1$ inclusive; and
\var{s56} must be a 56-bit signed value, i.e., a value in the range
$-2^{55}$ to $2^{55}-1$ inclusive.
\var{eness} must be a valid endianness symbol naming the endianness.
These procedures store the given value in the 3, 5, 6, or 7 bytes starting
at index \var{n} (zero-based) of \var{bytevector}.
Negative values are stored as their two's complement equivalent.
\section{Boxes\label{SECTBOXES}}
\index{boxes}Boxes are single-cell objects that are primarily useful for providing
an ``extra level of indirection.''
This extra level of indirection is typically used to allow more than one body
of code or data structure to share a \index{reference}reference, or \index{pointer}pointer, to an object.
For example, boxes may be used to implement \index{call-by-reference}\emph{call-by-reference} semantics
in an interpreter for a language employing this parameter passing discipline.
\index{\scheme{#&} (box prefix)}Boxes are written with
the prefix \scheme{#&} (pronounced ``hash-ampersand'').
For example, \scheme{#&(a b c)} is a box holding the list \scheme{(a b c)}.
The box syntax is disabled in an input stream after \scheme{#!r6rs} has
been seen by the reader, unless \scheme{#!chezscheme} has been seen more
recently.
%----------------------------------------------------------------------------
\entryheader
\formdef{box?}{\categoryprocedure}{(box? \var{obj})}
\returns \scheme{#t} if \var{obj} is a box, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noskip\schemedisplay
(box? '#&a) ;=> #t
(box? 'a) ;=> #f