From: Matt Wette <matt.wette@gmail.com>
To: Guile User <guile-user@gnu.org>
Subject: string-unfold demo
Date: Sat, 26 Aug 2017 10:38:14 -0700 [thread overview]
Message-ID: <8AE545E7-749B-4EF5-AFD8-7DA4340D1C39@gmail.com> (raw)
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"
next reply other threads:[~2017-08-26 17:38 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-26 17:38 Matt Wette [this message]
2017-08-28 8:56 ` string-unfold demo Alex Vong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8AE545E7-749B-4EF5-AFD8-7DA4340D1C39@gmail.com \
--to=matt.wette@gmail.com \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).