unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Alex Vong <alexvong1995@gmail.com>
To: Matt Wette <matt.wette@gmail.com>
Cc: Guile User <guile-user@gnu.org>
Subject: Re: string-unfold demo
Date: Mon, 28 Aug 2017 16:56:50 +0800	[thread overview]
Message-ID: <87shgcjcj1.fsf@gmail.com> (raw)
In-Reply-To: <8AE545E7-749B-4EF5-AFD8-7DA4340D1C39@gmail.com> (Matt Wette's message of "Sat, 26 Aug 2017 10:38:14 -0700")

[-- Attachment #1: Type: text/plain, Size: 3087 bytes --]

Hello Guilers,

Since I find this exercise interesting, I come up with another demo. The
trick is to think of string as a list of characters (like in Haskell)
and to use the fact that append can be written as an unfold.

Let's begin:

1. Use SRFI-1 and SRFI-26

  (use-modules (srfi srfi-1)
               (srfi srfi-26))

2. Implement append using unfold

  (define (append a b)
    (unfold null?
            car
            cdr
            a
            (const b)))

Test it:

  (append '(1 2 3) '(4 5 6))
  => (1 2 3 4 5 6)

3. Extend append to allow any number of arguments

  (define append
    (lambda args
      (define (%append a b)
        (unfold null?
                car
                cdr
                a
                (const b)))
      (fold-right %append '() args)))

Test it:

  (append '(1 2 3) '(4 5 6) '(7 8 9))
  => (1 2 3 4 5 6 7 8 9)

4. "Port" the procedure to "string"-land

First, notice 'unfold' takes a TAIL-GEN procedure to generate the tail,
while string-unfold takes a BASE string, not a procedure. So, let's
replace '(const b)' with 'b'. Next, replace 'null?' with 'string-null?',
'car' with '(cut string-ref <> 0)' and 'cdr' with
'(cut string-drop <> 1)'

  (define (string-append a b)
    (string-unfold string-null?
                   (cut string-ref <> 0)
                   (cut string-drop <> 1)
                   a
                   b))

Test it:

  (string-append "123" "456")
  => "456123"

Oops, we got the a and b reversed.

5. Reverse a and b

  (define (string-append a b)
    (string-unfold string-null?
                   (cut string-ref <> 0)
                   (cut string-drop <> 1)
                   b
                   a))

Test it:

  (string-append "123" "456")
  => "123456"

Now it works.

6. Extend again to allow any number of arguments

  (define string-append
    (lambda args
      (define (%string-append a b)
        (string-unfold string-null?
                       (cut string-ref <> 0)
                       (cut string-drop <> 1)
                       b
                       a))
      (fold-right %string-append "" args)))

Test it:

  (string-append "123" "456" "789")
  => "123456789"

DONE!

Cheers,
Alex

Matt Wette <matt.wette@gmail.com> writes:

> 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"

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

      reply	other threads:[~2017-08-28  8:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-26 17:38 string-unfold demo Matt Wette
2017-08-28  8:56 ` Alex Vong [this message]

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=87shgcjcj1.fsf@gmail.com \
    --to=alexvong1995@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=matt.wette@gmail.com \
    /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).