fork download
  1. ; double double words
  2.  
  3. (define (read-line . port)
  4. (define (eat p c)
  5. (if (and (not (eof-object? (peek-char p)))
  6. (char=? (peek-char p) c))
  7. (read-char p)))
  8. (let ((p (if (null? port) (current-input-port) (car port))))
  9. (let loop ((c (read-char p)) (line '()))
  10. (cond ((eof-object? c) (if (null? line) c (list->string (reverse line))))
  11. ((char=? #\newline c) (eat p #\return) (list->string (reverse line)))
  12. ((char=? #\return c) (eat p #\newline) (list->string (reverse line)))
  13. (else (loop (read-char p) (cons c line)))))))
  14.  
  15. (define (string-split sep str)
  16. (define (f cs xs) (cons (list->string (reverse cs)) xs))
  17. (let loop ((ss (string->list str)) (cs '()) (xs '()))
  18. (cond ((null? ss) (reverse (if (null? cs) xs (f cs xs))))
  19. ((char=? (car ss) sep) (loop (cdr ss) '() (f cs xs)))
  20. (else (loop (cdr ss) (cons (car ss) cs) xs)))))
  21.  
  22. (define line #f)
  23. (define number #f)
  24.  
  25. (define (cleanup str)
  26. (let loop ((cs (string->list str)) (zs (list)))
  27. (cond ((null? cs) (list->string (reverse zs)))
  28. ((char-alphabetic? (car cs))
  29. (loop (cdr cs) (cons (car cs) zs)))
  30. ((char-whitespace? (car cs))
  31. (loop (cdr cs) (cons #\space zs)))
  32. (else (loop (cdr cs) zs)))))
  33.  
  34. (define (read-word)
  35. (if (pair? line)
  36. (let ((word (car line)))
  37. (set! line (cdr line))
  38. word)
  39. (let ((input (read-line)))
  40. (if (eof-object? input)
  41. input
  42. (begin
  43. (set! line (string-split #\space (cleanup input)))
  44. (set! number (+ number 1))
  45. (read-word))))))
  46.  
  47. (define (double)
  48. (set! line "") (set! number 0)
  49. (let loop ((prev "") (word (read-word)))
  50. (when (not (eof-object? word))
  51. (when (string-ci=? prev word)
  52. (display number) (display " ")
  53. (display word) (newline))
  54. (loop word (read-word)))))
  55.  
  56. (double)
Success #stdin #stdout 0s 7268KB
stdin
This is a very, very good
good example of doubled
doubled words.
stdout
1 very
2 good
3 doubled