fork download
  1. ; double space
  2.  
  3. (define (double-space oldstr)
  4. (define (space? str idx)
  5. (char=? (string-ref str idx) #\space))
  6. (let ((oldlen (string-length oldstr)))
  7. (do ((oldidx 0 (+ oldidx 1))
  8. (count 0 (+ count (if (space? oldstr oldidx) 1 0))))
  9. ((= oldidx oldlen)
  10. (let* ((newlen (+ oldlen count))
  11. (newstr (make-string newlen #\space)))
  12. (do ((oldidx 0 (+ oldidx 1))
  13. (newidx 0 (+ newidx (if (space? newstr newidx) 2 1))))
  14. ((= oldidx oldlen) newstr)
  15. (string-set! newstr newidx (string-ref oldstr oldidx))))))))
  16.  
  17. (display (double-space "hello")) (newline)
  18. (display (double-space "hello hello")) (newline)
  19. (display (double-space "hello hello")) (newline)
  20. (display (double-space "hello hello")) (newline)
  21. (display (double-space "hello hello hello")) (newline)
  22. (display (double-space " hello ")) (newline)
  23. (display (double-space "")) (newline)
  24. (display (double-space " ")) (newline)
  25. (display (double-space " ")) (newline)
  26. (display (double-space " ")) (newline) (newline)
  27.  
  28. (define (double-space str)
  29. (let loop ((xs (string->list str)) (zs (list)))
  30. (if (null? xs) (list->string (reverse zs))
  31. (if (char=? (car xs) #\space)
  32. (loop (cdr xs) (cons #\space (cons #\space zs)))
  33. (loop (cdr xs) (cons (car xs) zs))))))
  34.  
  35. (display (double-space "hello")) (newline)
  36. (display (double-space "hello hello")) (newline)
  37. (display (double-space "hello hello")) (newline)
  38. (display (double-space "hello hello")) (newline)
  39. (display (double-space "hello hello hello")) (newline)
  40. (display (double-space " hello ")) (newline)
  41. (display (double-space "")) (newline)
  42. (display (double-space " ")) (newline)
  43. (display (double-space " ")) (newline)
  44. (display (double-space " ")) (newline)
Success #stdin #stdout 0.01s 42848KB
stdin
Standard input is empty
stdout
hello
hello  hello
hello    hello
hello      hello
hello  hello  hello
  hello  

  
    
      

hello
hello  hello
hello    hello
hello      hello
hello  hello  hello
  hello