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
|
<RefEntry id="split">
<!-- This file is generated automatically from the DSSSL source. -->
<!-- Do not edit this file! -->
<?html-filename split.html>
<RefMeta>
<RefEntryTitle>split</RefEntryTitle>
<RefMiscInfo Role="file">dblib.dsl</RefMiscInfo>
</RefMeta>
<RefNameDiv>
<RefName>split</RefName>
<RefPurpose>Splits string at whitespace and returns the resulting list of tokens</RefPurpose>
</RefNameDiv>
<RefSynopsisDiv><Title>Synopsis</Title>
<Synopsis>
(split str #!optional (whitespace '(#\space)))
</Synopsis>
</RefSynopsisDiv>
<RefSect1><Title>Description</Title>
<para>
Given a string containing delimited tokens, return a list
of the tokens in string form.
</para>
<variablelist>
<varlistentry><term><literal>str</literal></term>
<listitem>
<para>
The string to split.
</para>
</listitem>
</varlistentry>
<varlistentry><term><literal>whitespace</literal></term>
<listitem>
<para>
A list of characters that should
be treated as whitespace.
</para>
</listitem>
</varlistentry>
</variablelist>
</RefSect1>
<RefSect1><Title>Author</Title>
<para>
David Megginson, <dmeggins@uottawa.ca>
</para>
</RefSect1>
<RefSect1><Title>Source Code</Title>
<ProgramListing>
(define (split str #!optional (whitespace '(#\space)))
;; Splits string at whitespace and returns the resulting list of tokens
(let loop ((characters (string->list str)) ; Top-level recursive loop.
(current-word '())
(tokens '()))
; If there are no characters left,
; then we're done!
(cond ((null? characters)
; Is there a token in progress?
(if (null? current-word)
(reverse tokens)
(reverse (cons (list->string (reverse current-word))
tokens))))
; If there are characters left,
; then keep going.
(#t
(let ((c (car characters))
(rest (cdr characters)))
; Are we reading a space?
(cond ((member c whitespace)
(if (null? current-word)
(loop rest '() tokens)
(loop rest
'()
(cons (list->string (reverse current-word))
tokens))))
; We are reading a non-space
(#t
(loop rest (cons c current-word) tokens))))))))
</ProgramListing>
</RefSect1>
</RefEntry>
|