Racket's loop iteration system is extensible; we can extend Racket to produce a 'for' loop that can accumulate strings. For example:<div><br></div><div>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</div>
<div><div>;; An example of using for/fold/derived.</div><div>;;</div><div>;; for/string: a loop variant that produces a string:</div><div>(define-syntax (for/string stx)</div><div> (syntax-case stx ()</div><div> [(_ clauses . body)</div>
<div> (with-syntax ([original stx])</div><div> #'(list->string </div><div> (reverse</div><div> (for/fold/derived original</div><div> ([chars/rev '()])</div>
<div> clauses</div><div> (cons (let () . body)</div><div> chars/rev)))))]))</div></div><div><div>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</div>
<div></div></div><div><br></div><div>Hopefully we don't have to write this all out from scratch every time we use it; we should probably sequester this into a library and not need to look at it again. :)<br></div><div>
<br></div><div><br></div><div>But once we shuffle this in a library somewhere, we can use it like any of the other loop variations, and it makes your subsequent definitions nice and short:</div><div><br></div><div><div><div>
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</div><div></div></div></div><div><div>;; string -> string</div><div>;; Removes vowels from the input</div><div>(define (remove-vowels word)</div><div> (for/string ([ch (in-string word)]</div>
<div> #:when (not (member ch (list #\a #\e #\i #\o #\u))))</div><div> ch))</div></div><div><div><div><br></div><div><div><br></div><div>;; string -> string</div><div>;; Replaces spaces in the input with dashes</div>
<div>(define (replace-spaces word)</div><div> (for/string ([c (in-string word)])</div><div> (if (char=? c #\space) #\- c)))</div></div><div>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</div><div><br></div>
<div><br></div><div>Best of wishes!</div><div></div></div></div>