guile-user
[Top][All Lists]
Advanced

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

Re: Question about data structures


From: Zelphir Kaltstahl
Subject: Re: Question about data structures
Date: Sun, 22 Nov 2020 21:24:48 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0

Hi divoplade!

I know there is reverse and I think I did implement it before, when
working through SICP exercises. Thanks for that implementation and input
though!

I think the point I wanted to make is rather to avoid `reverse`
completely. If I had a vector, I could simply go by index backwards or
forwards without adding any runtime complexity. But a vector is of
defined length, not expandable like a list via cons, which means it
might not be the best idea under some circumstances. Other circumstances
make lists less ideal. If those circumstances appear together, then I
probably should be using another data structure. Or pay the price in
time complexity for some operations.

I just read, that Python "lists" are actually implemented as
https://en.wikipedia.org/wiki/Dynamic_array. So I guess a more specific
but less generally useful question is: "What do I use in Guile, if I
were using a dynamic array in Python?"

I almost never find myself reversing a Python "list". Probably because
it can be indexed in reverse order indices.

Best regards,
Zelphir


On 11/22/20 8:45 PM, divoplade wrote:
> Hello Zelphir!
>
> Le dimanche 22 novembre 2020 à 19:48 +0100, Zelphir Kaltstahl a écrit :
>> However, when I use the list in
>> reverse and ever need to output the lines in the list in their
>> original
>> order, I would first need to `reverse` the list again.
> There is a "reverse" function; you could implement it yourself as a
> tail-recursive function if you wanted (it's currently implemented in C,
> so my guess is it's even more efficient). You don't need vectors for
> that.
>
> (define (my-reverse-aux accumulation list)
>   (if (null? list)
>       accumulation
>       (my-reverse-aux (cons (car list) accumulation) (cdr list))))
>
> (define (my-reverse list)
>   (my-reverse-aux '() list))
>
> (my-reverse '(a b c d e f g h))
>
-- 
repositories: https://notabug.org/ZelphirKaltstahl




reply via email to

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