unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Panicz Maciej Godek <godek.maciek@gmail.com>
To: Dmitry Bogatov <KAction@gnu.org>
Cc: guile-user <guile-user@gnu.org>
Subject: Re: Uniq list in Guile
Date: Sat, 26 Oct 2013 10:03:44 +0200	[thread overview]
Message-ID: <CAMFYt2bA2m=yYeuhKgy6HOe_rmiJyf5=9_Ts+hzg9BHUqxBBhQ@mail.gmail.com> (raw)
In-Reply-To: <87d2mspkt3.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 1227 bytes --]

There's also a "delete-duplicates" function available that comes with
SRFI-1.
It has a different behaviour, because it uniques the whole list, not just
the groups of adjacent elements, and is a pure function, but usually can be
used to achieve the desired effect.
The manual claims that it has an O(n^2) complexity. There's a simple way to
decrease the complexity to linear time (+ sorting time, if one wishes to
preserve the order) using a hash table, like that:

(define (unique elements)
  (let ((result (make-hash-table)))
    (let loop ((i 0)(lst elements))
      (if (pair? lst)
          (begin
            (hash-set! result (car lst) i)
            (loop (1+ i) (cdr lst)))
          (map car (sort-list (hash-map->list cons result)
                              (lambda (a b) (< (cdr a) (cdr b)))))))))

Perhaps someone else's opinion would be useful here, but I don't
think that UNIX-style "uniq" procedure should be common to Scheme
programming, and in particular -- its mutable variant, which makes it
completely uncomposable (the UNIX variant is immutable, so it can be
used with pipes). Note that uniq has to be used with sort in order to
express the above concept (i.e. removing duplicate entries).

Regards,
M.

[-- Attachment #2: Type: text/html, Size: 1572 bytes --]

  reply	other threads:[~2013-10-26  8:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-26  7:12 Uniq list in Guile Dmitry Bogatov
2013-10-26  8:03 ` Panicz Maciej Godek [this message]
2013-10-26  9:06   ` Thien-Thi Nguyen
2013-10-26 12:05     ` Dmitry Bogatov
2013-10-26 10:16 ` Ian Price
2013-10-26 23:14   ` Ludovic Courtès

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='CAMFYt2bA2m=yYeuhKgy6HOe_rmiJyf5=9_Ts+hzg9BHUqxBBhQ@mail.gmail.com' \
    --to=godek.maciek@gmail.com \
    --cc=KAction@gnu.org \
    --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).