<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, &lt;dmeggins@uottawa.ca&gt;
</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>
