Re: useful string utility

From: Lars Thomas Hansen <lth_at_ccs.neu.edu>
Date: Mon, 22 Sep 1997 08:54:10 -0400

> > I had
> > earlier tried to do the same thing in scheme - with much frustration:
>
>Scheme is a wonderful language, but I agree with you that
>character manipulation in it is very painful.
>
>
>
> -- Erick

I don't understand what's so painful about it. The scheme procedure
below does the same thing as Erick's procedure, and is less than 1/2 the
lines of code.

Sure, there's a small performance hit because string-ref can be rather
slower than *p. On the other hand, Scheme characters are not
necessarily bytes (Gambit-C, for example, has Unicode characters) and a
Scheme implementation preserves that subtlety, whereas a typical C
implementation does not, being written either for bytes or for wide
characters.

Or are you merely lamenting the lack of a standard string-split as part
of the language definition?

--lars

(define (split-string s . rest)
  (let ((delimiters (if (null? rest)
                        (list #\space #\tab #\newline)
                        (string->list (car rest)))))
    (let iloop ((i 0) (l '()))
      (cond ((>= i (string-length s))
             (reverse l))
            ((memv (string-ref s i) delimiters)
             (iloop (+ i 1) l))
            (else
             (let jloop ((j (+ i 1)))
               (if (and (< j (string-length s))
                        (not (memv (string-ref s j) delimiters)))
                   (jloop (+ j 1))
                   (iloop (+ j 1) (cons (substring s i j) l)))))))))
Received on Mon Sep 22 1997 - 14:54:49 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST