I am trying to write a tail-recursive implementation of the function take-while
in Scheme (but this exercise can be done in another language as well). My first attempt was
(define (take-while p xs)
(if (or (null? xs)
(not (p (car xs))))
'()
(cons (car xs) (take-while p (cdr xs)))))
which works correctly but is not tail-recursive. My next attempt was
(define (take-while-tr p xs)
(let loop ((acc '())
(ys xs))
(if (or (null? ys)
(not (p (car ys))))
(reverse acc)
(loop (cons (car ys) acc) (cdr ys)))))
which is tail recursive but needs a call to reverse
as a last step in order to return the result list in the proper order.
I cannot come up with a solution that
- is tail-recursive,
- does not use
reverse
, - only uses lists as data structure (using a functional data structure like a Haskell's sequence which allows to append elements is not an option),
- has complexity linear in the size of the prefix, or at least does not have quadratic complexity (thanks to delnan for pointing this out).
Is there an alternative solution satisfying all the properties above? My intuition tells me that it is impossible to accumulate the prefix of a list in a tail-recursive fashion while maintaining the original order between the elements (i.e. without the need of using reverse to adjust the result) but I am not able to prove this.
Note
The solution using reverse
satisfies conditions 1, 3, 4.
1 Answer 1
Hmm, some cleverness continuations might work
First we write a function which takes a continuation, and returns a new continuation which cons
a value onto the result
(define cons-later
(lambda (c x)
(lambda (r)
(cons x (c r)))))
Now take-while
(define take-while
(lambda (pred xs)
(define worker
(lambda (c xs)
(if (pred (car xs))
(worker (cons-later c (car xs)) ; Make our new continuation
(cdr xs))
(c '())))) ; Call the continuation
(worker (lambda (x) x) xs)))
We still pay the piper at some point though; this version takes up a bit more room than with reverse.
This uses functions of course, but since your reverse implementation did and was still counted as satisfying 1 3 and 4 I'll just innocently whistle.
-
Very interesting (and, IMHO, elegant) solution. Somehow it confirms my intuition that one needs some temporary memory to accumulate the partial prefixes: in your solution this is achieved by building nested continuations; in the non-tail-recursive solution this is achieved by using the stack; in the tail-recursive solution this is achieved by storing a temporary list that is reversed at the end. But it is not possible to output and forget each element of the result as we scan the prefix.Giorgio– Giorgio2014年06月04日 19:41:26 +00:00Commented Jun 4, 2014 at 19:41
-
@Giorgio Indeed I'm flummoxed how to get something to that effectdaniel gratzer– daniel gratzer2014年06月04日 20:04:12 +00:00Commented Jun 4, 2014 at 20:04
-
I would like to discuss the topic in chat if you are interested (and if you can find the link to the chat).Giorgio– Giorgio2014年06月04日 20:30:41 +00:00Commented Jun 4, 2014 at 20:30
-
@Giorgio I just got that :/ If you're still online I'm happy to discuss this in chat :)daniel gratzer– daniel gratzer2014年06月05日 02:31:43 +00:00Commented Jun 5, 2014 at 2:31
reverse
you still have linear complexity, while appending at each steps makes the complexity quadratic.