I am coding with Racket for educational reasons.
I was given a task in which I should create a function that, without filter, would receive a list as an input and return another list only with the even numbers of the first list.
I came up with this recursive definition of an iterative process:
(define (add-even lista)
(define (iter lista accu)
(cond ((null? lista) accu)
((even? (car lista)) (iter (cdr lista)
(cons (car lista) accu)))
(else (iter (cdr lista) accu))))
(iter lista empty))
It works fine. However, I get the result in a reverse order, e.g.:
(add-even '(1 2 3 4 5 6 7))
>> '(6 4 2)
What should I do to have the output in the same order of appearance on the input?
I know how to do it with a reverse function. But that's not a very efficient way..
Of course you can do it without the
iter
procedure ...But I assume you're using that to keep your
add-even
procedure tail-recursive. If that's the case then ...Your
accu
can be a procedure (instead of a list) which fills in a "hole" in yourcons
chain. Instead of returningaccu
at the end of the computation, you fill in the last value, which in this case isempty
and initialize withidentity
instead.I used bolding to show the parts of your code I changed
So now you get tail-recursion and you build the list in forward order. I encourage you to step thru the evaluation of this to see how it works. This is continuation passing style.
And perhaps the procedure would be better if you renamed the vars a little bit
And it cleans up even a little more if you used a
named-let
... And it cleans up even more if we used
compose
andcurry
In Racket, built-in
for/list
with#:when
clause can also be used to create a short function: