unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
To: divoplade <d@divoplade.fr>, Guile User <guile-user@gnu.org>
Subject: Re: Question about data structures
Date: Sun, 22 Nov 2020 21:24:48 +0100	[thread overview]
Message-ID: <df2ed155-3477-986a-a6ae-dc2648fd56c4@posteo.de> (raw)
In-Reply-To: <3b07669d7286df2542796e309db101423bb3e147.camel@divoplade.fr>

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	other threads:[~2020-11-22 20:24 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-22 18:48 Question about data structures Zelphir Kaltstahl
2020-11-22 19:45 ` divoplade
2020-11-22 20:24   ` Zelphir Kaltstahl [this message]
2020-11-22 21:00     ` divoplade
2020-11-28  2:47       ` Zelphir Kaltstahl
2020-11-22 20:58   ` kwright
2020-11-22 22:50 ` Tim Van den Langenbergh
2020-11-23  3:42 ` Taylan Kammer
2020-11-23  4:42   ` John Cowan
2020-11-23 14:54   ` [EXT] " Thompson, David
2020-11-23  9:52 ` Neil Jerram
2020-11-23 12:24 ` Dr. Arne Babenhauserheide

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=df2ed155-3477-986a-a6ae-dc2648fd56c4@posteo.de \
    --to=zelphirkaltstahl@posteo.de \
    --cc=d@divoplade.fr \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).