unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
To: Taylan Kammer <taylan.kammer@gmail.com>
Cc: Guile User <guile-user@gnu.org>
Subject: Re: Writing a procedure in different style
Date: Sun, 13 Dec 2020 13:29:31 +0100	[thread overview]
Message-ID: <33683c10-5e29-8e4a-03a4-64852fbcf474@posteo.de> (raw)
In-Reply-To: <1385bd6a-c14e-76f1-6dfe-4cf50d192375@gmail.com>

Hello Taylan!

I tried your procedure and indeed it seems to work : )

I think what I had been missing before were 2 things:

1. I did not have the (if (null? rest) ...) parts, so I always tried to
directly make a recursive call, perhaps wrapped into a cons, append or
list. But if the rest was not null, then this would always add some
level of nesting to my result, when it finally returned, which I did not
want. The (if (null? rest) ...) part makes sure, that the empty list is
given instead.

2. The fact that I could simply cons onto the rest, because the
procedure works with arbitrarily nested lists anyway.

Thanks for your input!

Regards,
Zelphir

On 13.12.20 12:51, Taylan Kammer wrote:
> On 13.12.2020 08:06, Taylan Kammer wrote:
>>
>>    (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 '())))
>
> Correction, for third branch of the cond that didn't check if rest is
> null:
>
>   (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
>                    (if (null? rest)
>                        '()
>                        (traverse (car rest) (cdr rest))))]
>             [else
>              (traverse (cdr subtree) rest)])))
>
>        (traverse peg-tree '())))
>
>
> Taylan

-- 
repositories: https://notabug.org/ZelphirKaltstahl




  reply	other threads:[~2020-12-13 12:29 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
2020-12-13 11:51   ` Taylan Kammer
2020-12-13 12:29     ` Zelphir Kaltstahl [this message]
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=33683c10-5e29-8e4a-03a4-64852fbcf474@posteo.de \
    --to=zelphirkaltstahl@posteo.de \
    --cc=guile-user@gnu.org \
    --cc=taylan.kammer@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).