procedures.scm 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. (define (string-split-by-string input separator)
  2. ;; base case, the string is exhausted
  3. (cond [(= (string-length input) 0) '()]
  4. [else
  5. (let ([pos-of-separator (string-contains input separator)])
  6. (cond
  7. ;; if there is another separator in the string
  8. [pos-of-separator
  9. ;; we need to cut out the prefix until that string and
  10. ;; recur
  11. (let ([prefix (substring input 0 pos-of-separator)]
  12. [suffix (substring input (+ pos-of-separator (string-length separator)))])
  13. (cons prefix
  14. (string-split-by-string suffix separator)))]
  15. ;; if there is no occurrence of separator in the input
  16. ;; string
  17. [else (cons input '())]))]))
  18. (define (string-repeat str n)
  19. (define (iter port str n)
  20. (cond
  21. [(> n 0)
  22. (display str port)
  23. (iter port str (- n 1))]
  24. [else ""]))
  25. (call-with-output-string
  26. (λ (port)
  27. (iter port str n))))
  28. (define* (string-pad str width char #:key (padding-direction 'left))
  29. (let loop ((padded-string str))
  30. (cond
  31. [(< (string-length padded-string) width)
  32. (cond
  33. [(eq? padding-direction 'left) (loop (string-append char padded-string))]
  34. [(eq? padding-direction 'right) (loop (string-append padded-string char))]
  35. [else (error "padding-direction not left or right")])]
  36. [else padded-string])))