Matt Wette
2017-08-26 17:38:14 UTC
Just for kicks, to learn string-unfold, I made an ugly version of string-append:
(define (ugly-string-append . str-l)
;; p: seed |-> #t|#f predicate to indicate stop
(define (p seed) (null? seed))
;; f: seed |-> char output function
(define (f seed) (string-ref (cddar seed) (caar seed)))
;; g: seed |-> seed transition function
(define (g seed) (let* ((head (car seed)) (tail (cdr seed))
(ix (car head)) (ln (cadr head)) (st (cddr head)))
(if (eq? (1+ ix) ln) tail
(cons (cons* (1+ ix) ln st) tail))))
;; s: seed = ((ix1 ln1 . st1) (ix2 ln2 . st2) ...)
;; where ix is curr index, ln is string-length, and st is string
(define s (map (lambda (s) (cons* 0 (string-length s) s)) str-l))
(string-unfold p f g s))
(ugly-string-append "abc" "def") => "abcdef"
(define (ugly-string-append . str-l)
;; p: seed |-> #t|#f predicate to indicate stop
(define (p seed) (null? seed))
;; f: seed |-> char output function
(define (f seed) (string-ref (cddar seed) (caar seed)))
;; g: seed |-> seed transition function
(define (g seed) (let* ((head (car seed)) (tail (cdr seed))
(ix (car head)) (ln (cadr head)) (st (cddr head)))
(if (eq? (1+ ix) ln) tail
(cons (cons* (1+ ix) ln st) tail))))
;; s: seed = ((ix1 ln1 . st1) (ix2 ln2 . st2) ...)
;; where ix is curr index, ln is string-length, and st is string
(define s (map (lambda (s) (cons* 0 (string-length s) s)) str-l))
(string-unfold p f g s))
(ugly-string-append "abc" "def") => "abcdef"