fork download
  1. ;; (import (chicken string))
  2. ;; (import (chicken format))
  3. (define (f s)
  4. (define (aux acc cs)
  5. (if (null? cs)
  6. (reverse-list->string acc)
  7. (let ((c (car cs)) (cs (cdr cs)))
  8. (cond
  9. ((not (eq? c #\ )) (aux (cons c acc) cs))
  10. ((and (not (null? acc)) (eq? (car acc) #\ )) (aux acc cs))
  11. (else (aux (cons #\ (cons #\ (cons #\ (cons #\ acc)))) cs))))))
  12. (aux '() (string->list s)))
  13. (define (g s)
  14. (format #t "\"~A\" -> \"~A\"~%" s (f s)))
  15. (g "a b c")
  16. (g " a ")
  17. (g " a")
  18. (g "a ")
  19. (g " ")
  20. (g "")
  21.  
Success #stdin #stdout 0.01s 7728KB
stdin
Standard input is empty
stdout
"a b     c" -> "a    b    c"
" a " -> "    a    "
"  a" -> "    a"
"a  " -> "a    "
" " -> "    "
"" -> ""