fork(1) download
  1. ; abbreviated sentences
  2.  
  3. (define (abbrev sentence)
  4. (with-output-to-string (lambda ()
  5. (define (word head len prev)
  6. (display head)
  7. (when (positive? len) (display (number->string len)))
  8. (when prev (display prev)))
  9. (let loop ((cs (string->list sentence))
  10. (head #f) (len -1) (prev #f))
  11. (cond ((null? cs) ; end of sentence
  12. (when head (word head len prev)))
  13. ((char-alphabetic? (car cs)) ; in a word
  14. (if head
  15. (loop (cdr cs) head (+ len 1) (car cs))
  16. (loop (cdr cs) (car cs) -1 #f)))
  17. (else ; not in a word
  18. (when head (word head len prev))
  19. (display (car cs))
  20. (loop (cdr cs) #f 0 #f)))))))
  21.  
  22. (display (abbrev "A is one; Programming Praxis hello-goodbye."))
Success #stdin #stdout 0.02s 42848KB
stdin
Standard input is empty
stdout
A is o1e; P9g P4s h3o-g5e.