unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Taylan Kammer <taylan.kammer@gmail.com>
To: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>,
	Guile User <guile-user@gnu.org>
Subject: Re: Writing a procedure in different style
Date: Sun, 13 Dec 2020 08:06:53 +0100	[thread overview]
Message-ID: <2d6f348c-1e43-a5c4-1998-baa04b91bf8c@gmail.com> (raw)
In-Reply-To: <3760549b-b32f-fafc-1291-8e3eda7b3753@posteo.de>

On 12.12.2020 13:22, Zelphir Kaltstahl wrote:
> ~~~~
> (define find-in-tree*
>    (λ (peg-tree filter-proc)
> 
>      (define traverse
>        (λ (subtree cont)
>          (simple-format (current-output-port)
>                         "working with subtree ~a\n"
>                         subtree)
>          (cond
>           [(null? subtree) (cont)]
>           [(pair? (first subtree))
>            (traverse (first subtree)
>                      (λ () (traverse (cdr subtree) cont)))]
>           [(filter-proc (first subtree))
>            (cons subtree (cont))]
>           [else
>            (traverse (cdr subtree) cont)])))
> 
>      (traverse peg-tree (λ () '()))))
> ~~~~

 From what I can tell, the continuation argument is essentially used as 
a way of queuing up further work that is to be done.  Adding an element 
is done by wrapping it in yet another procedure that adds some work, and 
popping an element is done by applying it.

This means it should be possible to change it into an explicit data 
structure (e.g. simply a list).  Here's an attempt, which I have not 
tested in any way:

   (define find-in-tree*
      (λ (peg-tree filter-proc)

        (define traverse
          (λ (subtree rest)
            (simple-format (current-output-port)
                           "working with subtree ~a\n"
                           subtree)
            (cond
             [(null? subtree)
              (if (null? rest)
                  '()
                  (traverse (car rest) (cdr rest))]
             [(pair? (first subtree))
              (traverse (first subtree)
                        (cons (cdr subtree) rest))]
             [(filter-proc (first subtree))
              (cons subtree
                    (traverse (car rest) (cdr rest)))]
             [else
              (traverse (cdr subtree) rest)])))

        (traverse peg-tree '())))

In summary:

- I rename 'cont' to 'rest'

- I change
   (cont)
   to
   (if (null? rest) '() (traverse (car rest) (cdr rest)))

- I change
   (λ () (traverse (cdr subtree) cont))
   to
   (cons (cdr subtree) rest)

- I change the initial seed of
   (λ () '())
   to
   '()

And I think theoretically it should behave the same.


Taylan



  reply	other threads:[~2020-12-13  7:06 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-12 12:22 Writing a procedure in different style Zelphir Kaltstahl
2020-12-13  7:06 ` Taylan Kammer [this message]
2020-12-13 11:51   ` Taylan Kammer
2020-12-13 12:29     ` Zelphir Kaltstahl
2020-12-13 14:24       ` tomas
2020-12-13 15:01         ` Zelphir Kaltstahl
2020-12-13 15:43           ` tomas
2020-12-20 17:57             ` Zelphir Kaltstahl
2020-12-21 12:31               ` tomas
2020-12-21 21:29                 ` Zelphir Kaltstahl

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=2d6f348c-1e43-a5c4-1998-baa04b91bf8c@gmail.com \
    --to=taylan.kammer@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=zelphirkaltstahl@posteo.de \
    /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).