fork download
  1. ; ordered vowels
  2.  
  3. (define (vowel? c)
  4. (member c '(#\a #\e #\i #\o #\u #\A #\E #\I #\O #\U)))
  5.  
  6. (define (ordered-vowels? str)
  7. (let loop ((cs (string->list str)) (prev #\space))
  8. (cond ((null? cs) #t)
  9. ((not (vowel? (car cs))) (loop (cdr cs) prev))
  10. ((char<=? prev (car cs)) (loop (cdr cs) (car cs)))
  11. (else #f))))
  12.  
  13. (define (ordered-vowel-words filename)
  14. (with-input-from-file filename
  15. (lambda ()
  16. (let loop ((word (read-line)))
  17. (unless (eof-object? word)
  18. (when (ordered-vowels? word)
  19. (display word) (newline))
  20. (loop (read-line)))))))
  21.  
  22. (display (ordered-vowels? "afoot")) (newline)
  23.  
  24. (define (for-each-input reader proc . pof)
  25. (let* ((f? (and (pair? pof) (string? (car pof))))
  26. (p (cond (f? (open-input-file (car pof)))
  27. ((pair? pof) (car pof))
  28. (else (current-input-port)))))
  29. (do ((item (reader p) (reader p)))
  30. ((eof-object? item)
  31. (if f? (close-input-port p)))
  32. (proc item))))
  33.  
  34. (define (filter-input reader pred?)
  35. (lambda args
  36. (let loop ((item (apply reader args)))
  37. (if (or (eof-object? item) (pred? item)) item
  38. (loop (apply reader args))))))
  39.  
  40. (define (read-line . port)
  41. (define (eat p c)
  42. (if (and (not (eof-object? (peek-char p)))
  43. (char=? (peek-char p) c))
  44. (read-char p)))
  45. (let ((p (if (null? port) (current-input-port) (car port))))
  46. (let loop ((c (read-char p)) (line '()))
  47. (cond ((eof-object? c) (if (null? line) c (list->string (reverse line))))
  48. ((char=? #\newline c) (eat p #\return) (list->string (reverse line)))
  49. ((char=? #\return c) (eat p #\newline) (list->string (reverse line)))
  50. (else (loop (read-char p) (cons c line)))))))
  51.  
  52. (define (sorted? lt? xs)
  53. (cond ((null? xs) #t)
  54. ((null? (cdr xs)) #t)
  55. ((lt? (cadr xs) (car xs)) #f)
  56. (else (sorted? lt? (cdr xs)))))
  57.  
  58. (define (ordered-vowels? str)
  59. (sorted? char<? (filter vowel? (string->list str))))
  60.  
  61. (define (ordered-vowel-words filename)
  62. (for-each-input
  63. (filter-input read-line ordered-vowels?)
  64. (lambda (word) (display word) (newline))
  65. filename))
  66.  
  67. (display (ordered-vowels? "afoot")) (newline)
Success #stdin #stdout 0.01s 8800KB
stdin
Standard input is empty
stdout
#t
#t