[racket] String manipulation without regex opinion

From: Grant Rettke (grettke at acm.org)
Date: Tue Nov 6 14:59:30 EST 2012


Usually there are homework problems and stuff for tweaking strings but I
was just playing around for the fun of it. I wondered what is the "best
way" to do a couple of simple tasks but without regex. Here are two of
them. They are not optimized I just wrote them in a way that seemed the
simplest. What is a better way or different way?

#lang racket

(require rackunit)

;; string -> string
;; Removes vowels from the input
(define remove-vowels
  (lambda (word)
    (let ((test (lambda (c) (not (member c (list #\a #\e #\i #\o #\u)))))
          (ls (string->list word)))
      (list->string (filter test ls)))))

(check-equal? (remove-vowels "") "")
(check-equal? (remove-vowels "f") "f")
(check-equal? (remove-vowels "a e i o u") "    ")

;; string -> string
;; Replaces spaces in the input with dashes
(define replace-spaces
 (lambda (word)
   (let ((test (lambda (c) (if (equal? c #\space) #\- c)))
         (ls (string->list word)))
     (list->string (map test ls)))))

(check-equal? (replace-spaces "") "")
(check-equal? (replace-spaces " ") "-")
(check-equal? (replace-spaces "a b c") "a-b-c")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121106/24aae956/attachment.html>

Posted on the users mailing list.