December 6, 2013
This exercise cries out for a regular-expression solution. Here is an awk one-liner:
awk '/^[QWERTASDFGZXCVB]*$/' words
If you don’t want to use regular expressions, you could define the alphabet like this:
(define left (string->list "QWERTASDFGZXCVB"))
(define (left? word)
(all? (lambda (c) (member c left))
Then it’s just a matter of reading a word list and discarding those words that aren’t
(do ((word (read) (read))) ((eof-object? word))
(when (left? (symbol->string word))
(display word) (newline)))))
Either of the two functions prints the word list instantly; there is no discernible difference in the time taken, since the computation is I/O bound.
You can see the program at http://programmingpraxis.codepad.org/JKXQRIS5.
Pages: 1 2