[racket] newbie string handling problem

From: Phil Bewig (pbewig at gmail.com)
Date: Thu Mar 31 21:05:25 EDT 2011

; lnic -- last non-white is colon

(define (lnic? s) ; explode to list
  (let loop ((cs (reverse (string->list s))))
    (cond ((null? cs) #f)
          ((char-whitespace? (car cs)) (loop (cdr cs)))
          (else (char=? (car cs) #\:)))))

(define (lnic? s) ; index into string
  (let loop ((i (- (string-length s) 1)))
    (cond ((negative? i) #f)
          ((char-whitespace? (string-ref s i)) (loop (- i 1)))
          (else (char=? (string-ref s i) #\:)))))

(define (lnic? s) ; saved current character
  (let loop ((i (string-length s)))
    (if (zero? i)
        #f
        (let ((c (string-ref s (- i 1))))
          (if (char-whitespace? c)
              (loop (- i 1))
              (char=? c #\:))))))

(define (trim s) ; remove whitespace from end of string
  (let loop ((i (- (string-length s) 1)))
    (cond ((negative? i) "")
          ((char-whitespace? (string-ref s i)) (loop (- i 1)))
          (else (substring s 0 (+ i 1))))))

(define (lnic? s) ; trim, then check last character
  (let* ((s (trim s)) (len (string-length s)))
    (and (positive? len) (char=? (string-ref s (- len 1)) #\:))))

(define (lnic-test) ; returns (#f #t #t #t #f)
  (map lnic? '("abcde" "abcd:" "abcd:   " ":   " "   ")))

Pick the one you like the best.  The first version explodes the string to a
list of characters, as your post suggested, but apparently you had some
trouble making that work.  The second version repeatedly indexes into the
string, starting at the end, until it finds a non-white character, then
checks if that is a colon.  The third version shows a different way of
computing the index, and stores the current character in a local variable so
the lookup is only performed once, not twice.  The fourth version checks the
last character of the trimmed string.  I would probably write the second
version and skip the regular expression.

-- Phil (http://programmingpraxis.com)

On Thu, Mar 31, 2011 at 6:38 PM, Charles Hixson
<charleshixsn at earthlink.net>wrote:

> Hi,
> I'm trying to determine whether the last non-whitespace character of a
> string is a colon, and I haven't been able to figure out the best way of
> doing this.  In python I'd just trim the string and look at the character
> before the end, but while Racket seems to have a string-length function, it
> doesn't seem to have anything analogous to trim.  So how should it be done?
>
> The best I've come up with is to explode the string into a list of
> characters, and handle it that way, but that's clearly quite wasteful of
> both processing and RAM.  (Of course, part of it is that the best way I've
> figured to proceed from there is to then copy that list to another list
> checking at each step of the way to tell if I was done, and then if it was
> successful to join the resultant list back into a string.)
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20110331/e51ad52f/attachment.html>

Posted on the users mailing list.