guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Writing a procedure in different style


From: Zelphir Kaltstahl
Subject: Re: Writing a procedure in different style
Date: Sun, 13 Dec 2020 13:29:31 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Icedove/68.10.0

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 via email to

[Prev in Thread] Current Thread [Next in Thread]