source: project/chicken/branches/prerelease/manual/Unit extras @ 9424

Last change on this file since 9424 was 9424, checked in by Ivan Raikov, 12 years ago

Merged with trunk

File size: 21.2 KB
Line 
1[[tags: manual]]
2[[toc:]]
3
4== Unit extras
5
6This unit contains a collection of useful utility definitions.
7This unit is used by default, unless the program
8is compiled with the {{-explicit-use}} option.
9
10
11=== Lists
12
13
14==== alist-ref
15
16 [procedure] (alist-ref KEY ALIST [TEST [DEFAULT]])
17
18Looks up {{KEY}} in {{ALIST}} using {{TEST}} as the comparison function (or {{eqv?}} if
19no test was given) and returns the cdr of the found pair, or {{DEFAULT}} (which defaults to {{#f}}).
20
21
22==== alist-update!
23
24 [procedure] (alist-update! KEY VALUE ALIST [TEST])
25
26If the list {{ALIST}} contains a pair of the form {{(KEY . X)}}, then this procedure
27replaces {{X}} with {{VALUE}} and returns {{ALIST}}. If {{ALIST}} contains no such item, then
28{{alist-update!}} returns {{((KEY . VALUE) . ALIST)}}. The optional argument
29{{TEST}} specifies the comparison procedure to search a matching pair in {{ALIST}}
30and defaults to {{eqv?}}.
31
32
33==== atom?
34
35 [procedure] (atom? X)
36
37Returns {{#t}} if {{X}} is not a pair. This is identical to {{not-pair?}} from [[Unit srfi-1]] but
38kept for historical reasons.
39
40
41==== rassoc
42
43 [procedure] (rassoc KEY LIST [TEST])
44
45Similar to {{assoc}}, but compares {{KEY}} with the {{cdr}} of each pair in {{LIST}} using
46{{TEST}} as the comparison procedures (which defaults to {{eqv?}}.
47
48
49==== butlast
50
51 [procedure] (butlast LIST)
52
53Returns a fresh list with all elements but the last of {{LIST}}.
54
55
56==== chop
57
58 [procedure] (chop LIST N)
59
60Returns a new list of sublists, where each sublist contains {{N}}
61elements of {{LIST}}. If {{LIST}} has a length that is not
62a multiple of {{N}}, then the last sublist contains the remaining
63elements.
64
65<enscript highlight=scheme>
66(chop '(1 2 3 4 5 6) 2) ==> ((1 2) (3 4) (5 6))
67(chop '(a b c d) 3)     ==> ((a b c) (d))
68</enscript>
69
70
71
72==== compress
73
74 [procedure] (compress BLIST LIST)
75
76Returns a new list with elements taken from {{LIST}} with
77corresponding true values in the list {{BLIST}}.
78
79<enscript highlight=scheme>
80(define nums '(99 100 110 401 1234))
81(compress (map odd? nums) nums)      ==> (99 401)
82</enscript>
83
84
85==== flatten
86
87 [procedure] (flatten LIST1 ...)
88
89Returns {{LIST1 ...}} concatenated together, with nested lists
90removed (flattened).
91
92
93==== intersperse
94
95 [procedure] (intersperse LIST X)
96
97Returns a new list with {{X}} placed between each element.
98
99
100==== join
101
102 [procedure] (join LISTOFLISTS [LIST])
103
104Concatenates the lists in {{LISTOFLISTS}} with {{LIST}} placed
105between each sublist. {{LIST}} defaults to the empty list.
106
107<enscript highlight=scheme>
108(join '((a b) (c d) (e)) '(x y)) ==> (a b x y c d x y e)
109(join '((p q) () (r (s) t)) '(-))  ==> (p q - - r (s) t)
110</enscript>
111
112{{join}} could be implemented as follows:
113
114<enscript highlight=scheme>
115(define (join lstoflsts #!optional (lst '()))
116  (apply append (intersperse lstoflists lst)) )
117</enscript>
118
119
120==== shuffle
121
122 [procedure] (shuffle LIST)
123
124Returns {{LIST}} with its elements sorted in a random order.
125
126
127==== tail?
128
129 [procedure] (tail? X LIST)
130
131Returns true if {{X}} is one of the tails (cdr's) of {{LIST}}.
132
133
134
135=== String-port extensions
136
137
138==== call-with-input-string
139
140 [procedure] (call-with-input-string STRING PROC)
141
142Calls the procedure {{PROC}} with a single argument that is a
143string-input-port with the contents of {{STRING}}.
144
145
146==== call-with-output-string
147
148 [procedure] (call-with-output-string PROC)
149
150Calls the procedure {{PROC}} with a single argument that is a
151string-output-port.  Returns the accumulated output-string.
152
153
154==== with-input-from-string
155
156 [procedure] (with-input-from-string STRING THUNK)
157
158Call procedure {{THUNK}} with the current input-port temporarily
159bound to an input-string-port with the contents of {{STRING}}.
160
161
162==== with-output-to-string
163
164 [procedure] (with-output-to-string THUNK)
165
166Call procedure {{THUNK}} with the current output-port temporarily
167bound to a string-output-port and return the accumulated output string.
168
169
170
171=== Formatted output
172
173
174==== printf
175==== fprintf
176==== sprintf
177
178 [procedure] (fprintf PORT FORMATSTRING ARG ...)
179 [procedure] (printf FORMATSTRING ARG ...)
180 [procedure] (sprintf FORMATSTRING ARG ...)
181
182Simple formatted output to a given port ({{fprintf}}), the
183value of {{(current-output-port)}} ({{printf}}), or a string
184({{sprintf}}).  The {{FORMATSTRING}} can contain any sequence
185of characters.  There must be at least as many {{ARG}} arguments given as there are format directives that require an argument in {{FORMATSTRING}}.  Extra {{ARG}} arguments are ignored.  The character `~' prefixes special formatting directives:
186
187<table>
188<tr><td> ~%
189write newline character
190</td></tr><tr><td> ~N
191the same as {{~%}}
192</td></tr><tr><td> ~S
193write the next argument
194</td></tr><tr><td> ~A
195display the next argument
196</td></tr><tr><td> ~\n
197skip all whitespace in the format-string until the next non-whitespace character
198</td></tr><tr><td> ~B
199write the next argument as a binary number
200</td></tr><tr><td> ~O
201write the next argument as an octal number
202</td></tr><tr><td> ~X
203write the next argument as a hexadecimal number
204</td></tr><tr><td> ~C
205write the next argument as a character
206</td></tr><tr><td> ~~
207display `~'
208</td></tr><tr><td> ~!
209flush all pending output
210</td></tr><tr><td> ~?
211invoke formatted output routine recursively with the next two arguments as format-string and list of parameters
212</td></tr></table>
213
214
215==== format
216
217 [procedure] (format [DESTINATION] FORMATSTRING ARG ...)
218
219The parameters  {{FORMATSTRING}} and {{ARG ...}} are as for
220({{printf}}/{{sprintf}}/{{fprintf}}).
221
222The optional {{DESTINATION}}, when supplied, performs
223a ({{sprintf}}) for a  {{#f}},
224a ({{printf}}) for a {{#t}},
225and a ({{fprintf}}) for an output-port.
226When missing a ({{sprintf}}) is performed.
227
228
229
230=== Hash tables
231
232CHICKEN implements SRFI-69. For more information,
233see [[http://srfi.schemers.org/srfi-69/srfi-69.html|SRFI-69]].
234
235A setter for {{hash-table-ref}} is defined, so
236
237<enscript highlight=scheme>
238(set! (hash-table-ref HT KEY) VAL)
239</enscript>
240
241is equivalent to
242
243<enscript highlight=scheme>
244(hash-table-set! HT KEY VAL)
245</enscript>
246
247As an extension to SRFI-69, {{hash-table-update!}} and {{hash-table-update!/default}} return
248the new value (after applying the update procedure).
249
250==== hash-table-remove!
251
252 [procedure] (hash-table-remove! HASHTABLE PROC)
253
254Calls {{PROC}} for all entries in {{HASHTABLE}} with the key and value of each
255entry. If {{PROC}} returns true, then that entry is removed.
256
257
258
259=== Queues
260
261
262==== list->queue
263
264 [procedure] (list->queue LIST)
265
266Returns {{LIST}} converted into a queue, where the first element
267of the list is the same as the first element of the queue. The resulting
268queue may share memory with the list and the list should not be modified
269after this operation.
270
271
272==== make-queue
273
274 [procedure] (make-queue)
275
276Returns a newly created queue.
277
278
279==== queue?
280
281 [procedure] (queue? X)
282
283Returns {{#t}} if {{X}} is a queue, or {{#f}} otherwise.
284
285
286==== queue->list
287
288 [procedure] (queue->list QUEUE)
289
290Returns {{QUEUE}} converted into a list, where the first element
291of the list is the same as the first element of the queue. The resulting
292list may share memory with the queue object and should not be modified.
293
294
295==== queue-add!
296
297 [procedure] (queue-add! QUEUE X)
298
299Adds {{X}} to the rear of {{QUEUE}}.
300
301
302==== queue-empty?
303
304 [procedure] (queue-empty? QUEUE)
305
306Returns {{#t}} if {{QUEUE}} is empty, or {{#f}} otherwise.
307
308
309==== queue-first
310
311 [procedure] (queue-first QUEUE)
312
313Returns the first element of {{QUEUE}}. If {{QUEUE}} is empty
314an error is signaled
315
316
317==== queue-last
318
319 [procedure] (queue-last QUEUE)
320
321Returns the last element of {{QUEUE}}. If {{QUEUE}} is empty
322an error is signaled
323
324
325==== queue-remove!
326
327 [procedure] (queue-remove! QUEUE)
328
329Removes and returns the first element of {{QUEUE}}. If {{QUEUE}}
330is empty an error is signaled
331
332
333==== queue-push-back!
334
335 [procedure] (queue-push-back! QUEUE ITEM)
336
337Pushes an item into the first position of a queue, i.e. the next
338{{queue-remove!}} will return {{ITEM}}.
339
340
341==== queue-push-back-list!
342
343 [procedure] (queue-push-back-list! QUEUE LIST)
344
345Pushes the items in item-list back onto the queue,
346so that {{(car LIST)}} becomes the next removable item.
347
348
349
350=== Sorting
351
352
353==== merge
354
355 [procedure] (merge LIST1 LIST2 LESS?)
356 [procedure] (merge! LIST1 LIST2 LESS?)
357
358Joins two lists in sorted order. {{merge!}} is the destructive
359version of merge. {{LESS?  }} should be a procedure of two arguments,
360that returns true if the first argument is to be ordered before the
361second argument.
362
363
364==== sort
365
366 [procedure] (sort SEQUENCE LESS?)
367 [procedure] (sort! SEQUENCE LESS?)
368
369Sort {{SEQUENCE}}, which should be a list or a vector. {{sort!}}
370is the destructive version of sort.
371
372
373==== sorted?
374
375 [procedure] (sorted? SEQUENCE LESS?)
376
377Returns true if the list or vector {{SEQUENCE}} is already sorted.
378
379
380
381=== Random numbers
382
383
384==== random-seed
385
386 [procedure] (random-seed [SEED])
387
388Seeds the random number generator with {{SEED}} (an exact integer) or
389{{(current-seconds)}} if {{SEED}} is not given.
390
391
392==== random
393
394 [procedure] (random N)
395
396Returns an exact random integer from 0 to {{N}}-1.
397
398
399==== randomize
400
401 [procedure] (randomize [X])
402
403Set random-number seed. If {{X}} is not supplied, the current time is used.
404On startup (when the {{extras}} unit is initialized), the random number generator
405is initialized with the current time.
406
407
408
409=== Input/Output extensions
410
411
412==== make-input-port
413
414 [procedure] (make-input-port READ READY? CLOSE [PEEK])
415
416Returns a custom input port. Common operations on this
417port are handled by the given parameters, which should be
418procedures of no arguments. {{READ}} is called when the
419next character is to be read and should return a character or
420{{#!eof}}. {{READY?}} is called
421when {{char-ready?}} is called on this port and should return
422{{#t}} or {{#f}}.  {{CLOSE}} is called when the port is
423closed. {{PEEK}} is called when {{peek-char}} is called on this
424port and should return a character or {{#!eof}}.
425if the argument {{PEEK}} is not given, then {{READ}} is used
426instead and the created port object handles peeking automatically (by
427calling {{READ}} and buffering the character).
428
429
430==== make-output-port
431
432 [procedure] (make-output-port WRITE CLOSE [FLUSH])
433
434Returns a custom output port. Common operations on this port are handled
435by the given parameters, which should be procedures.  {{WRITE}} is
436called when output is sent to the port and receives a single argument,
437a string.  {{CLOSE}} is called when the port is closed and should
438be a procedure of no arguments. {{FLUSH}} (if provided) is called
439for flushing the output port.
440
441
442==== pretty-print
443
444 [procedure] (pretty-print EXP [PORT])
445 [procedure] (pp EXP [PORT])
446
447Print expression nicely formatted. {{PORT}} defaults to the value
448of {{(current-output-port)}}.
449
450
451==== pretty-print-width
452(Parameter) Specifies the maximal line-width for pretty printing, after which line
453wrap will occur.
454
455==== read-byte
456==== write-byte
457
458 [procedure] (read-byte [PORT])
459 [procedure] (write-byte BYTE [PORT])
460
461Read/write a byte to the port given in {{PORT}}, which default to the values
462of {{(current-input-port)}} and {{(current-output-port)}}, respectively.
463
464==== read-file
465
466 [procedure] (read-file [FILE-OR-PORT [READER [MAXCOUNT]]])
467
468Returns a list containing all toplevel expressions
469read from the file or port {{FILE-OR-PORT}}. If no argument is given,
470input is read from the port that is the current value of {{(current-input-port)}}.
471After all expressions are read, and if the argument is a port, then the port will
472not be closed. The {{READER}} argument specifies the procedure used to read
473expressions from the given file or port and defaults to {{read}}. The reader
474procedure will be called with a single argument (an input port).
475If {{MAXCOUNT}} is given then only up to {{MAXCOUNT}} expressions will be read in.
476
477
478==== read-line
479==== write-line
480
481 [procedure] (read-line [PORT [LIMIT]])
482 [procedure] (write-line STRING [PORT])
483
484Line-input and -output. {{PORT}} defaults to the value of
485{{(current-input-port)}} and {{(current-output-port)}},
486respectively. If the optional argument {{LIMIT}} is given and
487not {{#f}}, then {{read-line}} reads at most {{LIMIT}}
488characters per line. {{read-line}} returns a string without the terminating newline and {{write-line}} adds a terminating newline  before outputting.
489
490
491==== read-lines
492
493 [procedure] (read-lines [PORT [MAX]])
494
495Read {{MAX}} or fewer lines from {{PORT}}. {{PORT}}
496defaults to the value of {{(current-input-port)}}. {{PORT}} may optionally be
497a string naming a file. Returns a list of strings, each string representing a line read, not including any line separation character(s).
498
499
500==== read-string
501==== read-string!
502==== write-string
503
504 [procedure] (read-string [NUM [PORT]])
505 [procedure] (read-string! NUM STRING [PORT [START]])
506 [procedure] (write-string STRING [NUM [PORT]]
507
508Read or write {{NUM}} characters from/to {{PORT}}, which defaults to the
509value of {{(current-input-port)}} or {{(current-output-port)}}, respectively.
510If {{NUM}} is {{#f}} or not given, then all data
511up to the end-of-file is read, or, in the case of {{write-string}} the whole
512string is written. If no more input is available, {{read-string}} returns the
513empty string. {{read-string!}} reads destructively into the given {{STRING}} argument,
514but never more characters that would fit into {{STRING}}. If {{START}} is given, then
515the read characters are stored starting at that position.
516{{read-string!}} returns the actual number of characters read.
517
518
519==== read-token
520
521 [procedure] (read-token PREDICATE [PORT])
522
523Reads characters from {{PORT}} (which defaults to the value of {{(current-input-port)}})
524and calls the procedure {{PREDICATE}} with each character until {{PREDICATE}} returns
525false. Returns a string with the accumulated characters.
526
527
528==== with-error-output-to-port
529
530 [procedure] (with-error-output-to-port PORT THUNK)
531
532Call procedure {{THUNK}} with the current error output-port
533temporarily bound to {{PORT}}.
534
535
536==== with-input-from-port
537
538 [procedure] (with-input-from-port PORT THUNK)
539
540Call procedure {{THUNK}} with the current input-port temporarily
541bound to {{PORT}}.
542
543
544==== with-output-to-port
545
546 [procedure] (with-output-to-port PORT THUNK)
547
548Call procedure {{THUNK}} with the current output-port temporarily
549bound to {{PORT}}.
550
551
552
553=== Strings
554
555
556==== conc
557
558 [procedure] (conc X ...)
559
560Returns a string with the string-represenation of all arguments concatenated
561together. {{conc}} could be implemented as
562
563<enscript highlight=scheme>
564(define (conc . args)
565  (apply string-append (map ->string args)) )
566</enscript>
567
568
569
570==== ->string
571
572 [procedure] (->string X)
573
574Returns a string-representation of {{X}}.
575
576
577==== string-chop
578
579 [procedure] (string-chop STRING LENGTH)
580
581Returns a list of substrings taken by ''chopping'' {{STRING}} every {{LENGTH}}
582characters:
583
584<enscript highlight=scheme>
585(string-chop "one two three" 4)  ==>  ("one " "two " "thre" "e")
586</enscript>
587
588
589
590==== string-chomp
591
592 [procedure] (string-chomp STRING [SUFFIX])
593
594If {{STRING}} ends with {{SUFFIX}}, then this procedure returns a copy of its first argument with the suffix
595removed, otherwise returns {{STRING}} unchanged. {{SUFFIX}} defaults to {{"\n"}}.
596
597
598==== string-compare3
599
600 [procedure] (string-compare3 STRING1 STRING2)
601 [procedure] (string-compare3-ci STRING1 STRING2)
602
603Perform a three-way comparison between the {{STRING1}} and {{STRING2}},
604returning either {{-1}} if {{STRING1}} is lexicographically less
605than {{STRING2}}, {{0}} if it is equal, or {{1}} if it s greater.
606{{string-compare3-ci}} performs a case-insensitive comparison.
607
608
609==== string-intersperse
610
611 [procedure] (string-intersperse LIST [STRING])
612
613Returns a string that contains all strings in {{LIST}} concatenated
614together.  {{STRING}} is placed between each concatenated string and
615defaults to {{" "}}.
616
617<enscript highlight=scheme>
618(string-intersperse '("one" "two") "three")
619</enscript>
620
621is equivalent to
622
623<enscript highlight=scheme>
624(apply string-append (intersperse '("one" "two") "three"))
625</enscript>
626
627
628==== string-split
629
630 [procedure] (string-split STRING [DELIMITER-STRING [KEEPEMPTY]])
631
632Split string into substrings separated by the given delimiters. If
633no delimiters are specified, a string comprising the tab, newline and space characters
634is assumed. If the
635parameter {{KEEPEMPTY}} is given and not {{#f}}, then empty
636substrings are retained:
637
638<enscript highlight=scheme>
639(string-split "one  two  three") ==> ("one" "two" "three")
640(string-split "foo:bar::baz:" ":" #t) ==> ("foo" "bar" "" "baz" "")
641</enscript>
642
643
644==== string-translate
645
646 [procedure] (string-translate STRING FROM [TO])
647
648Returns a fresh copy of {{STRING}} with characters matching
649{{FROM}} translated to {{TO}}.  If {{TO}} is omitted, then
650matching characters are removed. {{FROM}} and {{TO}} may be
651a character, a string or a list. If both {{FROM}} and {{TO}}
652are strings, then the character at the same position in {{TO}}
653as the matching character in {{FROM}} is substituted.
654
655
656==== string-translate*
657
658 [procedure] (string-translate* STRING SMAP)
659
660Substitutes elements of {{STRING}} according to {{SMAP}}.
661{{SMAP}} should be an association-list where each element of the list
662is a pair of the form {{(MATCH \. REPLACEMENT)}}. Every occurrence of
663the string {{MATCH}} in {{STRING}} will be replaced by the string
664{{REPLACEMENT}}:
665
666<enscript highlight=scheme>
667(string-translate*
668  "<h1>this is a \"string\"</h1>"
669  '(("<" . "&lt;") (">" . "&gt;") ("\"" . "&quot;")) )
670=>  "&lt;h1&gt;this is a &quot;string&quot;&lt;/h1&gt;"
671</enscript>
672
673
674==== substring=?
675
676 [procedure] (substring=? STRING1 STRING2 [START1 [START2 [LENGTH]]])
677 [procedure] (substring-ci=? STRING1 STRING2 [START1 [START2 [LENGTH]]])
678
679Returns {{#t}} if the strings {{STRING1}} and {{STRING2}} are equal, or
680{{#f}} otherwise.
681The comparison starts at the positions {{START1}} and {{START2}} (which default
682to 0), comparing {{LENGTH}} characters (which defaults to the minimum of the remaining
683length of both strings).
684
685
686==== substring-index
687
688 [procedure] (substring-index WHICH WHERE [START])
689 [procedure] (substring-index-ci WHICH WHERE [START])
690
691Searches for first index in string {{WHERE}} where string
692{{WHICH}} occurs.  If the optional argument {{START}} is given,
693then the search starts at that index.  {{substring-index-ci}}
694is a case-insensitive version of {{substring-index}}.
695
696
697
698=== Combinators
699
700
701==== any?
702
703 [procedure] (any? X)
704
705Ignores its argument and always returns {{#t}}. This is actually useful sometimes.
706
707
708==== constantly
709
710 [procedure] (constantly X ...)
711
712Returns a procedure that always returns the values {{X ...}} regardless of the number and value of its arguments.
713
714<enscript highlight=scheme>
715(constantly X) <=> (lambda args X)
716</enscript>
717
718
719==== complement
720
721 [procedure] (complement PROC)
722
723Returns a procedure that returns the boolean inverse of {{PROC}}.
724
725<enscript highlight=scheme>
726(complement PROC) <=> (lambda (x) (not (PROC x)))
727</enscript>
728
729
730==== compose
731
732 [procedure] (compose PROC1 PROC2 ...)
733
734Returns a procedure that represents the composition of the
735argument-procedures {{PROC1 PROC2 ...}}.
736
737<enscript highlight=scheme>
738(compose F G) <=> (lambda args
739                      (call-with-values
740                         (lambda () (apply G args))
741                         F))
742</enscript>
743
744{{(compose)}} is equivalent to {{values}}.
745
746
747==== conjoin
748
749 [procedure] (conjoin PRED ...)
750
751Returns a procedure that returns {{#t}} if its argument satisfies the
752predicates {{PRED ...}}.
753<enscript highlight=scheme>
754((conjoin odd? positive?) 33)   ==>  #t
755((conjoin odd? positive?) -33)  ==>  #f
756</enscript>
757
758
759==== disjoin
760
761 [procedure] (disjoin PRED ...)
762
763Returns a procedure that returns {{#t}} if its argument satisfies any
764predicate {{PRED ...}}.
765<enscript highlight=scheme>
766((disjoin odd? positive?) 32)    ==>  #t
767((disjoin odd? positive?) -32)   ==>  #f
768</enscript>
769
770
771
772==== each
773
774 [procedure] (each PROC ...)
775
776Returns a procedure that applies {{PROC ...}} to its arguments, and returns the result(s)
777of the last procedure application. For example
778
779<enscript highlight=scheme>
780(each pp eval)
781</enscript>
782
783is equivalent to
784
785<enscript highlight=scheme>
786(lambda args
787  (apply pp args)
788  (apply eval args) )
789</enscript>
790
791{{(each PROC)}} is equivalent to {{PROC}} and {{(each)}} is equivalent to
792{{noop}}.
793
794
795==== flip
796
797 [procedure] (flip PROC)
798
799Returns a two-argument procedure that calls {{PROC}} with its
800arguments swapped:
801<enscript highlight=scheme>
802(flip PROC) <=> (lambda (x y) (PROC y x))
803</enscript>
804
805
806==== identity
807
808 [procedure] (identity X)
809
810Returns its sole argument {{X}}.
811
812
813==== project
814
815 [procedure] (project N)
816
817Returns a procedure that returns its {{N}}th argument (starting from 0).
818
819
820==== list-of
821
822 [procedure] (list-of PRED)
823
824Returns a procedure of one argument that returns {{#t}} when
825applied to a list of elements that all satisfy the predicate procedure
826{{PRED}}, or {{#f}} otherwise.
827
828<enscript highlight=scheme>
829((list-of even?) '(1 2 3))   ==> #f
830((list-of number?) '(1 2 3)) ==> #t
831</enscript>
832
833
834==== noop
835
836 [procedure] (noop X ...)
837
838Ignores it's arguments, does nothing and returns an unspecified value.
839
840
841==== o
842
843 [procedure] (o PROC ...)
844
845A single value version of {{compose}} (slightly faster). {{(o)}} is equivalent
846to {{identity}}.
847
848=== Binary searching
849
850
851==== binary-search
852
853 [procedure] (binary-search SEQUENCE PROC)
854
855Performs a binary search in {{SEQUENCE}}, which should be a sorted
856list or vector.  {{PROC}} is called to compare items in the sequence,
857should accept a single argument and return an exact integer: zero if the
858searched value is equal to the current item, negative if the searched
859value is ''less'' than the current item, and positive otherwise.
860Returns the index of the found value or {{#f}} otherwise.
861
862Previous: [[Unit eval]]
863
864Next: [[Unit srfi-1]]
Note: See TracBrowser for help on using the repository browser.