It is very easy to use Scheme's reverse
function, for example after creating a list in reverse order with (cons new-obj my-list)
rather than (append my-list (list new-obj))
.
However, I'm wondering how efficient that part would be. If a Scheme list is a singly linked list (as I assume) that would imply traveling through the whole list at least once to reach the last element, isn't it? And this would require reverse
to somehow create the backlinks along the way?
OTOH in a doubly-linked list one would simply traverse the list from the end to the start, which would be more efficient.
My question is: if I have a situation where a program generates a list (in reverse order) and at some point processes the whole list is it worth bothering to implement that processing in a way that it can handle the list in reverse order, or is it no penalty to simply use reverse
first?
This is for Guile Scheme, and Guile 1.8 to be specific (if it makes a difference).