unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* string-unfold demo
@ 2017-08-26 17:38 Matt Wette
  2017-08-28  8:56 ` Alex Vong
  0 siblings, 1 reply; 2+ messages in thread
From: Matt Wette @ 2017-08-26 17:38 UTC (permalink / raw)
  To: Guile User

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"






^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-08-28  8:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-26 17:38 string-unfold demo Matt Wette
2017-08-28  8:56 ` Alex Vong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).