[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Writing a procedure in different style
From: |
Taylan Kammer |
Subject: |
Re: Writing a procedure in different style |
Date: |
Sun, 13 Dec 2020 08:06:53 +0100 |
User-agent: |
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.5.1 |
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
- Writing a procedure in different style, Zelphir Kaltstahl, 2020/12/12
- Re: Writing a procedure in different style,
Taylan Kammer <=
- Re: Writing a procedure in different style, Taylan Kammer, 2020/12/13
- Re: Writing a procedure in different style, Zelphir Kaltstahl, 2020/12/13
- Re: Writing a procedure in different style, tomas, 2020/12/13
- Re: Writing a procedure in different style, Zelphir Kaltstahl, 2020/12/13
- Re: Writing a procedure in different style, tomas, 2020/12/13
- Re: Writing a procedure in different style, Zelphir Kaltstahl, 2020/12/20
- Re: Writing a procedure in different style, tomas, 2020/12/21
- Re: Writing a procedure in different style, Zelphir Kaltstahl, 2020/12/21