-
Notifications
You must be signed in to change notification settings - Fork 2
/
srfi-101.html
986 lines (829 loc) · 32.2 KB
/
srfi-101.html
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--
SPDX-FileCopyrightText: 2009 David Van Horn
SPDX-License-Identifier: MIT
-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SRFI 101: Purely Functional Random-Access Pairs and Lists</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="/srfi.css" type="text/css" />
<style type="text/css">
body {
width: 7in;
margin: 30pt;
}
.proto {
font-weight: bold;
}
</style>
</head>
<body>
<!-- This commented out text is for the brittle SRFI tools -->
<!--
<H1>Title</H1>
Purely Functional Random-Access Pairs and Lists
<H1>Author</H1>
David Van Horn
<H1>Status</H1>
This SRFI is currently in ``final'' status.
-->
<!-- This is the real, valid XHTML text -->
<h1>Title</h1>
<p>Purely Functional Random-Access Pairs and Lists</p>
<h1>Author</h1>
<p>David Van Horn</p>
<h1>Status</h1>
<p>This SRFI is currently in <em>final</em> status. Here is <a href="https://srfi.schemers.org/srfi-process.html">an explanation</a> of each status that a SRFI can hold. To provide input on this SRFI, please send email to <code><a href="mailto:srfi+minus+101+at+srfi+dotschemers+dot+org">srfi-101@<span class="antispam">nospam</span>srfi.schemers.org</a></code>. To subscribe to the list, follow <a href="https://srfi.schemers.org/srfi-list-subscribe.html">these instructions</a>. You can access previous messages via the mailing list <a href="https://srfi-email.schemers.org/srfi-101">archive</a>.</p>
<ul>
<li>Received: 2009-09-09</li>
<li>Draft: 2009-09-16--2009-11-16</li>
<li>Revised: 2009-09-20</li>
<li>Revised: 2009-11-13</li>
<li>Finalized: 2013-02-24</li>
</ul>
<h1>Table of contents</h1>
<ul>
<li><a href="#Abstract">Abstract</a></li>
<li><a href="#Issues">Issues</a></li>
<li><a href="#Rationale">Rationale</a></li>
<li><a href="#Specification">Specification</a>
<ul>
<li>
<a href="#RandomAccessLists">Random-access pairs and
lists</a>
</li>
<li>
<a href="#RepresentationConversion">Representation
conversion</a>
</li>
<li>
<a href="#ImplementationRequirements">Implementation
requirements</a>
</li>
</ul>
</li>
<li><a href="#ReferenceImplementation">Reference Implementation</a></li>
<li><a href="#References">References</a></li>
<li><a href="#Acknowledgements">Acknowledgements</a></li>
<li><a href="#Copyright">Copyright</a></li>
</ul>
<h1><a name="Abstract">Abstract</a></h1>
<p>
Random-access lists [<a href="#note-1">1</a>] are a purely functional
data structure for representing lists of values. A random-access list
may act as a drop in replacement for the usual linear-access pair and
list data structures
(<code>pair?</code>, <code>cons</code>, <code>car</code>, <code>cdr</code>),
which additionally supports fast index-based addressing and updating
(<code>list-ref</code>, <code>list-set</code>). The impact is a whole
class of purely-functional algorithms expressed in terms of
index-based list addressing become feasible compared with their
linear-access list counterparts.
</p>
<p>
This document proposes a library API for purely functional
random-access lists consistent with the R<sup>6</sup>RS
[<a href="#note-2">2</a>] base library and list utility standard
library [<a href="#note-3">3</a>].
</p>
<h1><a name="Issues">Issues</a></h1>
<div>
<ul>
<li>
Procedure names have been chosen to be consistent with R6RS, even
though in some cases such as <code>list-ref</code>
and <code>list-tail</code> the choice seems poor since they
include the prefix <code>list-</code> even though they do not
operate on lists, but chains of pairs, i.e. lists and improper
lists, and arbitrary objects, respectively. Although the names
have remained the same, the descriptions have been corrected
(e.g. using <em>pair</em> or <em>obj</em> instead of <em>list</em>
for parameter names). Should the names be changed as well?
</li>
<li>
To what extent should standard Scheme procedures and syntax that
consume or construct lists be included in this proposal? For
example, should all of the <code>(rnrs base)</code> library that
deals with lists be included? By my count this would mean adding:
<code>lambda</code>, <code>apply</code>,
<code>vector->list</code>, <code>list->vector</code>,
<code>string->list</code>, and <code>list->string</code>. I am
inclined to add these. Should all of the <code>(rnrs
lists)</code> library be included? These procedures are easily
defined in terms of what's given here, and no perfomance advantage
is gained by implementig them "under the hood" using the data
structures in the reference implementation. I am
inclined <em>not</em> to include them.
</li>
<li>
Should a <code>car+cdr</code> procedure be added?
</li>
<li>
Should the current <code>syntax</code> and <code>procedures</code>
sub-libraries be included?
</li>
</ul>
</div>
<h1><a name="Rationale">Rationale</a></h1>
<p>
Functional programming and list hacking go together like peanut butter
and jelly, eval and apply, syntax and semantics, or cursing and
recursing. But the traditional approach to implementing pairs and
lists results in index-based access (<code>list-ref</code>) requiring
time proportional the index being accessed. Moreover, indexed-based
functional update (<code>list-set</code>) becomes so inefficient as to
be nearly unspeakable. Instead, programmers revert the imperatives of
the state; they use a stateful data structure and imperative
algorithms.
</p>
<p>
This SRFI intends to improve the situation by offering an alternative
implementation strategy based on Okasaki's purely functional
random-access lists [<a href="#note-1">1</a>]. Random-access pairs
and lists can be used as a replacement for traditional, linear-access
pairs and lists with no asymptotic loss of efficiency. In other
words, the typical list and pair operations such
as <code>cons</code>, <code>car</code>, and <code>cdr</code>, all
operate in <em>O(1)</em> time as usual. However, random-access lists
additionally support index-based access and functional update
operations that are asymptotically cheaper; <em>O(log(n))</em> for
random-access lists versus <em>O(n)</em> for linear-access lists,
where <em>n</em> is the length of the list being access or updated.
As such, many purely functional index-based list algorithms become
feasible by using a random-access list representation for pairs and
lists.
</p>
<p>
The requirements of this SRFI have been designed in such a way as to
admit portable library implementations of this feature, such as the
reference implementation, while at the same time admit more radical
implementations that embrace random-access pairs as the fundamental
pair representation.
</p>
<h1><a name="Specification">Specification</a></h1>
<h2><a name="RandomAccessLists">Random-access pairs and lists</a></h2>
<p>
A <em>random-access pair</em> (or just <em>pair</em>) is a compound
structure with two fields called the car and the cdr fields
(consistent with the historical naming of pair fields in Scheme).
Pairs are created by the procedure <code>cons</code>. The car and cdr
fields are accessed by the procedures <code>car</code>
and <code>cdr</code>.
</p>
<p>
Pairs are used primarily to represents lists. A list can be defined
recursively as either the empty list or a pair whose cdr is a list.
More precisely, the set of lists is defined as the smallest
set <em>X</em> such that</p>
<ul>
<li>The empty list is in <em>X</em>.</li>
<li>If <em>list</em> is in <em>X</em>, then any pair whose cdr field
contains <em>list</em> is also in <em>X</em>.</li>
</ul>
<p>
The objects in the car fields of successive pairs of a list are the
elements of the list. For example, a two-element list is a pair whose
car is the first element and whose cdr is a pair whose car is the
second element and whose cdr is the empty list. The length of a list
is the number of elements, which is the same as the number of pairs.
</p>
<p>
The empty list is a special object of its own type. It is not a
pair. It has no elements and its length is zero.
</p>
<blockquote>
<p>
<em>Note:</em> The above definitions imply that all lists have
finite length and are terminated by the empty list.
</p>
</blockquote>
<p>
A chain of pairs is defined recursively as either a non-pair object or
a pair whose cdr is a chain of pairs (Note: <em>every value</em> is a
chain of pairs). A chain of pairs ending in the empty list is a list.
A chain of pairs not ending in the empty list is called an improper
list. Note that an improper list is not a list. Whether a given pair
is a list depends upon what is stored in the cdr field.
</p>
<p>
The external representation of pairs is not specified by this SRFI,
however the examples below do use the typical notation for writing
pair and list values.
</p>
<p>
Random-access pairs and lists are specified to be fully functional,
or, to use the term from the academic literature, <em>fully
persistent</em> [<a href="#note-1">1</a>]. Full persistence means
that all operations on random-access lists, notably
including <code>cons</code>, <code>list-ref</code>, <code>list-set</code>,
and <code>list-ref/update</code>, are specified
</p>
<ol>
<li>
not to mutate any of their arguments; perforce
</li>
<li>
to be safe to execute concurrently on shared arguments; and
</li>
<li>
to suffer no degradation of performance as a consequence of the
history of operations carried out to produce their arguments
(except as it is reflected in the lengths of those arguments); but
permitted
</li>
<li>
to produce results that share structure with their arguments.
</li>
</ol>
<p>
It is usually taken for granted that standard Scheme lists have these
properties. This SRFI explicitly specifies that random-access lists
share them.
</p>
<p class="proto">
syntax: <code>(quote <em>datum</em>)</code>
</p>
<p>
<em>Syntax:</em> <Datum> should be a syntactic datum.
</p>
<p>
<em>Semantics:</em> <code>(quote <datum>)</code> evaluates to
the datum value represented by <datum> (see
section <a href="http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-7.html#node_sec_4.3">4.3</a>
of R6RS). This notation is used to include constants.
</p>
<p>
When the datum value represented by <datum> contains pair
structure, <code>quote</code> produces random-access pairs.
</p>
<pre>
(quote a) ⇒ a
(quote #(a b c)) ⇒ #(a b c)
(quote (+ 1 2)) ⇒ (+ 1 2)
</pre>
<p>
As noted in
section <a href="http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-7.html#node_sec_4.3.5">4.3.5</a>
of R6RS, <code>(quote <datum>)</code> may be abbreviated
as <code>'<datum></code>:
</p>
<pre>
'"abc" ⇒ "abc"
'145932 ⇒ 145932
'a ⇒ a
'#(a b c) ⇒ #(a b c)
'() ⇒ ()
'(+ 1 2) ⇒ (+ 1 2)
'(quote a) ⇒ (quote a)
''a ⇒ (quote a)
</pre>
<p>
As noted in
section <a href="http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-8.html#node_sec_5.10">5.10</a>
of R6RS, constants are immutable.
</p>
<blockquote>
<p>
<em>Note:</em> Different constants that are the value
of <code>quote</code> expression may share the same locations.
</p>
</blockquote>
<p class="proto">
procedure: <code>(equal? <em>obj<sub>1</sub></em> <em>obj<sub>2</sub></em>)
→ <em>bool</em></code>
</p>
<p>
The <code>equal?</code> predicate returns <code>#t</code> if and only
if the (possibly infinite) unfoldings of its arguments into regular
trees are equal as ordered trees.
</p>
<p>
The <code>equal?</code> predicate treats pairs and vectors as nodes
with outgoing edges, uses <code>string=?</code> to compare strings,
uses <code>bytevector=?</code> to compare bytevectors, and
uses <code>eqv?</code> to compare other nodes.
</p>
<pre>
(equal? 'a 'a) ⇒ #t
(equal? '(a) '(a)) ⇒ #t
(equal? '(a (b) c)
'(a (b) c)) ⇒ #t
(equal? "abc" "abc") ⇒ #t
(equal? 2 2) ⇒ #t
(equal? (make-vector 5 'a)
(make-vector 5 'a)) ⇒ #t
(equal? '#vu8(1 2 3 4 5)
(u8-list->bytevector
'(1 2 3 4 5)) ⇒ #t
(equal? (lambda (x) x)
(lambda (y) y)) ⇒ <em>unspecified</em>
(let* ((x (list 'a))
(y (list 'a))
(z (list x y)))
(list (equal? z (list y x))
(equal? z (list x x)))) ⇒ <em>(#t #t)</em>
</pre>
<p class="proto">
procedure: <code>(pair? <em>obj</em>) → <em>bool</em></code>
</p>
<p>
Returns <code>#t</code> if <em>obj</em> is a pair, and otherwise
returns <code>#f</code>. This operation must take <em>O(1)</em> time.
</p>
<pre>
(pair? '(a . b)) ⇒ #t
(pair? '(a b c)) ⇒ #t
(pair? '()) ⇒ #f
(pair? '#(a b)) ⇒ #f
</pre>
<p class="proto">
procedure: <code>(cons <em>obj<sub>1</sub></em> <em>obj<sub>2</sub></em>)
→ <em>pair</em></code>
</p>
<p>
Returns a newly allocated pair whose car is <em>obj<sub>1</sub></em>
and whose cdr is <em>obj<sub>2</sub></em>. The pair is guaranteed to
be different (in the sense of <code>eqv?</code>) from every existing
object. This operation must take <em>O(1)</em> time.
</p>
<pre>
(cons 'a '()) ⇒ (a)
(cons '(a) '(b c d)) ⇒ ((a) b c d)
(cons "a" '(b c)) ⇒ ("a" b c)
(cons 'a 3) ⇒ (a . 3)
(cons '(a b) 'c) ⇒ ((a b) . c)
</pre>
<p class="proto">
procedure: <code>(car <em>pair</em>) → <em>obj</em></code>
</p>
<p>
Returns the contents of the car field of <em>pair</em>. This
operation must take <em>O(1)</em> time.
</p>
<pre>
(car '(a b c)) ⇒ a
(car '((a) b c d)) ⇒ (a)
(car '(1 . 2)) ⇒ 1
(car '()) &assertion exception
</pre>
<p class="proto">
procedure: <code>(cdr <em>pair</em>) → <em>obj</em></code>
</p>
<p>
Returns the contents of the cdr field of <em>pair</em>. This
operation must take <em>O(1)</em> time.
</p>
<pre>
(cdr '((a) b c d)) ⇒ (b c d)
(cdr '(1 . 2)) ⇒ 2
(cdr '()) &assertion exception
</pre>
<p class="proto">
procedure: <code>(caar <em>pair</em>) → <em>obj</em></code><br/>
procedure: <code>(cadr <em>pair</em>) → <em>obj</em></code><br/>
...<br/>
procedure: <code>(cdddar <em>pair</em>) → <em>obj</em></code><br/>
procedure: <code>(cddddr <em>pair</em>) → <em>obj</em></code><br/>
</p>
<p>
These procedures are compositions of <code>car</code>
and <code>cdr</code>, where for example <code>caddr</code> could be
defined by<br/>
<code>(define caddr (lambda (x) (car (cdr (cdr x)))))</code>.
</p>
<p>
Arbitrary compositions, up to four deep, are provided. There are
twenty-eight of these procedures in all. These operations must
take <em>O(1)</em> time.
</p>
<p class="proto">
procedure: <code>(null? <em>obj</em>) → <em>bool</em></code>
</p>
<p>
Returns <code>#t</code> if <em>obj</em> is the empty
list, <code>#f</code> otherwise. This procedure is equivalent
to the <code>null?</code> procedure of the R6RS base library.
</p>
<p class="proto">
procedure: <code>(list? <em>obj</em>) → <em>bool</em></code>
</p>
<p>
Returns <code>#t</code> if <em>obj</em> is a list, <code>#f</code>
otherwise. By definition, all lists are chains of pairs that have
finite length and are terminated by the empty list. This operation
must take time bounded by <em>O(log(n))</em>, where <em>n</em> is the
number of pairs in the chain forming the potential list.
</p>
<pre>
(list? '(a b c)) ⇒ #t
(list? '()) ⇒ #t
(list? '(a . b)) ⇒ #f
</pre>
<p class="proto">
procedure: <code>(list <em>obj</em> ...) → <em>list</em></code>
</p>
<p>
Returns a newly allocated list of its arguments. This operation must
take time bounded by <em>O(n)</em>, where <em>n</em> is the number of
arguments to <code>list</code>.
</p>
<pre>
(list 'a (+ 3 4) 'c) ⇒ (a 7 c)
(list) ⇒ ()
</pre>
<p class="proto">
procedure: <code>(make-list <em>k</em>)
→ <em>list</em></code><br />
procedure: <code>(make-list <em>k</em> <em>obj</em>)
→ <em>list</em></code>
</p>
<p>
Returns a newly allocated list of <em>k</em> elements. If a second
argument is given, then each element is initialized
to <em>obj</em>. Otherwise the initial contents of each element is
unspecified. This operation must take time and space bounded
by <em>O(log(k))</em>.
</p>
<pre>
(make-list 5 0) ⇒ (0 0 0 0 0)
</pre>
<p class="proto">
procedure: <code>(length <em>list</em>) → <em>k</em></code>
</p>
<p>
Returns the length of <em>list</em>. This operation must take time
bounded by <em>O(log(n))</em>, where <em>n</em> is the length of the
list.
</p>
<pre>
(length '(a b c)) ⇒ 3
(length '(a (b) (c))) ⇒ 3
(length '()) ⇒ 0
</pre>
<p class="proto">
procedure: <code>(length<=? <em>obj</em> <em>k</em>)
→ <em>bool</em></code>
</p>
<p>
Returns true if <em>obj</em> is a chain of at least <em>k</em> pairs
and false otherwise. This operation must take time bounded
by <em>O(log(min(k,n)))</em>, where <em>n</em> is the length of the
chain of pairs.
</p>
<pre>
(length<=? 'not-a-list 0) ⇒ #t
(length<=? '(a . b) 0) ⇒ #t
(length<=? '(a . b) 1) ⇒ #t
(length<=? '(a . b) 2) ⇒ #f
</pre>
<p class="proto">
procedure: <code>(append <em>list</em> ... <em>obj</em>)
→ <em>obj</em></code>
</p>
<p>
Returns a chain of pairs consisting of the elements of the
first <em>list</em> followed by the elements of the other lists, with
<em>obj</em> as the cdr of the final pair. An improper list results if
<em>obj</em> is not a list. This operation must take time bounded
by <em>O(log(n))</em>, where <em>n</em> is the total number of elements
in the given lists.
</p>
<pre>
(append '(x) '(y)) ⇒ (x y)
(append '(a) '(b c d)) ⇒ (a b c d)
(append '(a (b)) '((c))) ⇒ (a (b) (c))
(append '(a b) '(c . d)) ⇒ (a b c . d)
(append '() 'a) ⇒ a
</pre>
<p class="proto">
procedure: <code>(reverse <em>list</em>) → <em>list</em></code>
</p>
<p>
Returns a newly allocated list consisting of the element
of <em>list</em> in reverse order. This operation must take time
bounded by <em>O(n)</em> where <em>n</em> is the length of the list.
</p>
<pre>
(reverse '(a b c)) ⇒ (c b a)
(reverse '(a (b c) 'd '(e (f)))) ⇒ ((e (f)) d (b c) a)
</pre>
<p class="proto">
procedure: <code>(list-tail <em>obj</em> <em>k</em>)
→ <em>obj</em></code>
</p>
<p>
<em>Obj</em> should be a chain of pairs with a count of at
least <em>k</em>. The <code>list-tail</code> procedure returns the
object obtained by omitting the first <em>k</em> elements
in <em>obj</em>. This operation must take time bounded
by <em>O(log(min(k,n)))</em>, where <em>n</em> is the length of the
chain of pairs.
</p>
<pre>
(list-tail '(a b c d) 0) ⇒ (a b c d)
(list-tail '(a b c d) 2) ⇒ (c d)
(list-tail 'not-a-list 0) ⇒ not-a-list
</pre>
<p>
<em>Implementation responsibilities:</em> The implementation must
check that <em>obj</em> is a chain of pairs whose count is at
least <em>k</em>.
</p>
<p class="proto">
procedure: <code>(list-ref <em>pair</em> <em>k</em>)
→ <em>obj</em></code>
</p>
<p>
<em>Pair</em> must be a chain of pairs whose count is at least <em>k +
1</em>. The <code>list-ref</code> procedure returns the <em>k</em>th
element of <em>pair</em>. This operation must take time bounded
by <em>O(min(k,log(n)))</em>, where <em>n</em> is the length of the
chain of pairs.
</p>
<pre>
(list-ref '(a b c d) 2) ⇒ c
</pre>
<p>
<em>Implementation responsibilities:</em> The implementation must
check that <em>pair</em> is a chain of pairs whose count is at
least <em>k + 1</em>.
</p>
<p class="proto">
procedure: <code>(list-set <em>pair</em> <em>k</em> <em>obj</em>)
→ <em>obj</em></code>
</p>
<p>
<em>Pair</em> must be a chain of pairs whose count is at least <em>k +
1</em>. The <code>list-set</code> procedure returns the chain of
pairs obtained by replacing the <em>k</em>th element
with <em>obj</em>. This operation must take time bounded
by <em>O(min(k,log(n)))</em>, where <em>n</em> is the length of the
chain of pairs.
</p>
<pre>
(list-set '(a b c d) 2 'x) ⇒ (a b x d)
</pre>
<p>
<em>Implementation responsibilities:</em> The implementation must
check that <em>pair</em> is a chain of pairs whose count is at
least <em>k + 1</em>.
</p>
<p class="proto">
procedure: <code>(list-ref/update <em>pair</em> <em>k</em> <em>proc</em>)
→ <em>obj<sub>1</sub></em> <em>obj</em><sub>2</sub></code>
</p>
<p>
Returns the same results as:
</p>
<pre>
(values (list-ref pair k)
(list-set pair k (proc (list-ref pair k))))
</pre>
<p>
but it may be implemented more efficiently.
</p>
<pre>
(list-ref/update '(7 8 9 10) 2 -) ⇒ 9 (7 8 -9 10)
</pre>
<p class="proto">
procedure: <code>(map <em>proc</em> <em>list<sub>1</sub></em> <em>list<sub>2</sub></em>
...) → <em>list</em></code>
</p>
<p>
The <em>list</em>s should all have the same length. <em>Proc</em>
should accept as many arguments as there are <em>list</em>s and return
a single value.
</p>
<p>
The <code>map</code> procedure applies <em>proc</em> element-wise to
the elements of the <em>list</em>s and returns a list of the results,
in order. <em>Proc</em> is always called in the same dynamic
environment as <code>map</code> itself. The order in
which <em>proc</em> is applied to the elements of the <em>list</em>s
is unspecified.
</p>
<pre>
(map cadr '((a b) (d e) (g h))) ⇒ (b e h)
(map (lambda (n) (expt n n))
'(1 2 3 4 5))
⇒ (1 4 27 256 3125)
(map + '(1 2 3) (4 5 6)) ⇒ (5 7 9)
(let ((count 0))
(map (lambda (ignored)
(set! count (+ count 1))
count)
'(a b))) ⇒ (1 2) or (2 1)
</pre>
<p>
<em>Implementation responsibilities:</em> The implementation should
check that the <em>list</em>s all have the same length. The
implementation must check the restrictions on <em>proc</em> to the
extent performed by applying it as described. An implementation may
check whether <em>proc</em> is an appropriate argument before applying
it.
</p>
<p class="proto">
procedure: <code>(for-each <em>proc</em> <em>list<sub>1</sub></em> <em>list<sub>2</sub></em>
...) → <em>unspecified</em></code>
</p>
<p>
The lists should all have the same length. <em>Proc</em> should accept
as many arguments as there are lists.
</p>
<p>
The <code>for-each</code> procedure applies <em>proc</em> element-wise
to the elements of the <em>list</em>s for its side effects, in order
from the first element to the last. <em>Proc</em> is always called in
the same dynamic environment as <code>for-each</code> itself. The
return values of <code>for-each</code> are unspecified.
</p>
<pre>
(let ((v (make-vector 5)))
(for-each (lambda (i)
(vector-set! v i (* i i)))
'(0 1 2 3 4))
v) ⇒ #(0 1 4 9 16)
(for-each (lambda (x) x) '(1 2 3 4)) ⇒ unspecified
(for-each even? '()) ⇒ unspecified
</pre>
<p>
<em>Implementation responsibilities:</em> The implementation should
check that the <em>list</em>s all have the same length. The
implementation must check the restrictions on <em>proc</em> to the
extent performed by applying it as described. An implementation may
check whether <em>proc</em> is an appropriate argument before applying
it.
</p>
<blockquote>
<p>
<em>Note:</em> Implementations of <code>for-each</code> may or may
not tail-call <em>proc</em> on the last element.
</p>
</blockquote>
<h2><a name="RepresentationConversion">Representation
conversion</a></h2>
<p class="proto">
procedure: <code>(random-access-list->linear-access-list <em>ra-list</em>)
→ <em>la-list</em></code><br />
procedure: <code>(linear-access-list->random-access-list <em>la-list</em>)
→ <em>ra-list</em></code>
</p>
<p>
These procedures convert between (potentially) distinct
representations of lists. To avoid confusion, parameters
named <em>ra-list</em> range over lists represented with random-access
lists, i.e. objects satisfying the <code>list?</code> predicate
described above, while parameters named <em>la-list</em> range over
lists represented with the more traditional linear-access lists,
i.e. objects satisfying the <code>list?</code> predicate of R6RS. In
systems that represent all lists as random-access lists, these
conversions may simply be list identity procedures.
</p>
<h2><a name="ImplementationRequirements">Implementation
requirements</a></h2>
<p>
Random-access pairs must be disjoint from all other base types with
the possible exception of (linear-access) pairs.</p>
<p>
The external representation of random-access pairs is unspecified.
The behavior of <code>equal?</code> when given a random-access pair
and a sequential-access pair is unspecified in implementations with
disjoint representations.
</p>
<p>
The behavior of <code>eq?</code> and <code>eqv?</code> on
random-access pairs must be the same as that for pairs, vectors, or
records. Namely, two random-access pair objects are <code>eq?</code>
if and only if they are <code>eqv?</code>, and they
are <code>eqv?</code> if and only if they refer to the same location
in the store.
</p>
<p>
All argument checking for each operation must be done within the time
bounds given for that operation.
</p>
<p>
Implementations are encouraged, but not required, to support
random-access pairs and lists as their primary pair and list
representation. In such an implementation, the external
representation of random-access pairs and list should be as described
in
section <a href="http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-7.html#node_sec_4.3.2">4.3.2
(Pairs and lists)</a> of R<sup>6</sup>RS, the behavior of equivalence
predicates on random-access pairs should be as described in section
<a href="http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_idx_436">11.5
(Equivalence predicates)</a> of R<sup>6</sup>RS, and so on. In short,
all pairs should be random-access pairs.
</p>
<p>
Implementations supporting SRFI Libraries [<a href="#note-4">4</a>]
and SRFI 101 must provide the following libraries:
</p>
<pre>
(srfi :101) ; Composite libraries
(srfi :101 random-access-lists)
(srfi :101 random-access-lists procedures) ; Procedures only
(srfi :101 random-access-lists syntax) ; Syntax only
</pre>
<h1>
<a name="ReferenceImplementation">Reference Implementation</a>
</h1>
<p>
A portable R6RS library <a href="srfi-101.sls">reference
implementation</a> and <a href="srfi-101-tests.sls">test suite</a> are
provided. The library has been tested successfully using Ikarus
(0.0.3), Larceny (0.97), and PLT Scheme (4.2.1.7).
</p>
<p>
A PLT Scheme specific library that implements purely functional
random-access lists, but with an API designed to be consistent with
PLT's list libraries rather than R6RS, is available as the RaList
package on Planet [<a href="#note-5">5</a>].
</p>
<h1>
<a name="References">References</a>
</h1>
<dl>
<dt class="ref"><a name="note-1">[1]</a> Purely Functional
Random-Access Lists</dt>
<dd>Chris Okasaki, <em>Functional Programming Languages and
Computer Architecture</em>, June 1995, pages 86-95.<br/>
<a href="https://doi.org/10.1145/224164.224187">https://doi.org/10.1145/224164.224187</a>
</dd>
<dt class="ref"><a name="note-2">[2]</a> Revised<sup>6</sup>
Report on the Algorithmic Language Scheme</dt>
<dd>Michael Sperber, <em>et al.</em> (Editors)<br/>
<a href="http://www.r6rs.org/">http://www.r6rs.org/</a>
</dd>
<dt class="ref"><a name="note-3">[3]</a> Revised<sup>6</sup>
Report on the Algorithmic Language Scheme, Standard Libraries</dt>
<dd>Michael Sperber, <em>et al.</em> (Editors)<br/>
<a href="http://www.r6rs.org/">http://www.r6rs.org/</a>
</dd>
<dt class="ref"><a name="note-4">[4]</a> SRFI 97: SRFI Libraries</dt>
<dd>David Van Horn<br/>
<a href="https://srfi.schemers.org/srfi-97/">http://srfi.schemers.org/srfi-97/</a></dd>
<dt class="ref"><a name="note-5">[5]</a> PLaneT: Purely Functional Random-Access Lists</dt>
<dd>David Van Horn<br/>
<a href="https://docs.racket-lang.org/ralist/index.html">https://docs.racket-lang.org/ralist/index.html</a></dd>
</dl>
<h1>
<a name="Acknowledgements">Acknowledgments</a>
</h1>
<p>
I am grateful to the members of the
<a href="http://www.ccs.neu.edu/research/prl/">Northeastern University
Programming Research Laboratory</a>
and <a href="https://www.racket-lang.org/">PLT</a> (and their intersection)
for discussions during the pre-draft development of this SRFI and the
library that preceded it. We are all indebted to Okasaki for his
elegant solution to this problem. Much of the specification is
adapted from text intended to belong to the Scheme community; I thank
the editors and authors of the RnRS series collectively for their
efforts. I am grateful to Donovan Kolbly for serving as SRFI editor
and to Taylor R Campbell, Robert Bruce Findler, Aubrey Jaffer, Shiro
Kawai, and Alexey Radul for discussion during the draft period. I
thank William D Clinger, Robert Bruce Findler, and Abdulaziz Ghuloum
for help writing the implementations of <code>quote</code> specific to
Larceny, Ikarus, and PLT, respectively. Support was provided by the
National Science Foundation under Grant #0937060 to the Computing
Research Association for the CIFellow Project.
</p>
<h1>
<a name="Copyright">Copyright</a>
</h1>
<p>
Copyright (C) David Van Horn 2009. All Rights Reserved.
</p>
<p>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
</p>
<p>
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
</p>
<p>
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</p>
<hr />
<address>Editor: <a href="mailto:srfi+minus+editors+at+srfi+dot+schemers+dot+org">Donovan Kolbly</a></address>
<p>
<a href="http://validator.w3.org/check/referer"><img
style="border:0;width:88px;height:31px"
src="http://www.w3.org/Icons/valid-xhtml10-blue"
alt="Valid XHTML 1.0!" height="31" width="88" /></a>
</p>
</body></html>