unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: kwright@free-comp-shop.com
To: divoplade <d@divoplade.fr>
Cc: guile-user@gnu.org
Subject: Re: Question about data structures
Date: Sun, 22 Nov 2020 15:58:08 -0500	[thread overview]
Message-ID: <87r1olw1lr.fsf@fcs22.keithdiane.us> (raw)
In-Reply-To: <3b07669d7286df2542796e309db101423bb3e147.camel@divoplade.fr> (message from divoplade on Sun, 22 Nov 2020 20:45:05 +0100)

divoplade <d@divoplade.fr> writes:

> 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))

There is also a reverse! procedure.
  (reverse! '(a b c)) 
  $1 = (c b a)

I have not actually looked at the code, but I assume from the "!"
in the name and a decent respect for competence of the programmers,
that it uses the well-known algorithm to reverse a list by destructive
update.

This algorithm is still O(n), but who cares?  It is at least O(n)
to read n lines, no matter how how you read them.  The cost is
amortised and is only O(1) per line.  The thing to avoid is "cons".
The my-reverse procedure above still does O(n) times cons, which uses
storage, and may call garbage collection.

The destructive update algorithm does O(n) times update one storage
location and a couple of registers, which is trivial compared to
reading a line.

Just remember not to save pointers into the original non-reversed list,
because it gets smashed.  (You can still save pointers to the _members_
of the list.)

     -- Keith,

Programmer in Chief, Free Computer Shop,
http://www.free-comp-shop.com/
Food, Shelter, Source code

   



  parent reply	other threads:[~2020-11-22 20:58 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
2020-11-22 21:00     ` divoplade
2020-11-28  2:47       ` Zelphir Kaltstahl
2020-11-22 20:58   ` kwright [this message]
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=87r1olw1lr.fsf@fcs22.keithdiane.us \
    --to=kwright@free-comp-shop.com \
    --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).