An ordered word has its letters in alphabetical order. Write a function that returns the longest ordered word in a dictionary file.
$ cat low.scm (use-modules (ice-9 rdelim)) (define (ordered-word wd) ; Return true iff the letters in the given word are in ; alphabetical order. (let ((n (- (string-length wd) 1))) (if (< n 1) #t (let loop ((fst-i (- n 1)) (snd-c (string-ref wd n))) (if (< fst-i 0) #t (let ((fst-c (string-ref wd fst-i))) (if (char>? fst-c snd-c) #f (loop (- fst-i 1) fst-c)))))))) (define (longest-ordered-word dict-file) ; Return the longest ordered word in the given file. (call-with-input-file dict-file (lambda (p) (let loop ((wd (read-line p)) (low "") (low-n 0)) (if (eof-object? wd) low (let ((n (string-length wd)) (next-wd (read-line p))) (if (and (> n low-n) (ordered-word wd)) (loop next-wd wd n) (loop next-wd low low-n)))))))) (format #t "~a~%" (longest-ordered-word "/usr/share/dict/words")) $ guile low.scm Babbitt $