guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

string-unfold demo


From: Matt Wette
Subject: string-unfold demo
Date: Sat, 26 Aug 2017 10:38:14 -0700

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"






reply via email to

[Prev in Thread] Current Thread [Next in Thread]