root/utils.scm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(define-module (utils)
  #:use-module (srfi srfi-1)  ;; map/fold
  #:export (repeat-str
            repeat
            intersperse
            string-substitute))

(define* (repeat-str n str #:optional (acc ""))
  (if (> n 0)
      (repeat-str (- n 1) str (string-append acc str))
      acc))

(define (repeat n value)
  (list-tabulate n (lambda (_) value)))

(define (intersperse value base-list)
  (fold-right (lambda (current acc)
                (cons value (cons current acc)))
              (list value)
              base-list))

(define (string-substitute str search substitute start-recursive)
  (define found (string-contains str search))
  (define result
    (if found
        (cons #t (string-replace str
                                 substitute
                                 found (+ found (string-length search))))
        (cons #f str)))
  (if (and (car result) start-recursive)
      (string-substitute (cdr result) search substitute start-recursive)
      (cdr result)))