Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"A mind is a terrible thing to have leaking out your ears." -- The League of Sadistic Telepaths


devel / comp.lang.lisp / Re: sort behavior is strange

SubjectAuthor
* sort behavior is strangeJinsong Zhao
+* Re: sort behavior is strangeLieven Marchand
|+- Re: sort behavior is strangeTom Russ
|`- Re: sort behavior is strangeKaz Kylheku
`- Re: sort behavior is strangeKaz Kylheku

1
sort behavior is strange

<u1rhm2$kkfo$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17383&group=comp.lang.lisp#17383

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jszhao@yeah.net (Jinsong Zhao)
Newsgroups: comp.lang.lisp
Subject: sort behavior is strange
Date: Thu, 20 Apr 2023 22:18:49 +0800
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <u1rhm2$kkfo$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 20 Apr 2023 14:19:46 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c251652f863f3d0cd926d1520d926646";
logging-data="676344"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/4IrPbuF+PnJaK7Tq6XpxX"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.10.0
Cancel-Lock: sha1:TCIg3Y6qsoKOEK9rP7cUg3kTLMU=
Content-Language: en-US
 by: Jinsong Zhao - Thu, 20 Apr 2023 14:18 UTC

Hi there,

I hope to sort an associate list with #'sort. I tried something as follows:

* (setf zzz (pairlis '(a b c d e) '(2 4 5 1 3)))
((E . 3) (D . 1) (C . 5) (B . 4) (A . 2))
* (stable-sort zzz #'< :key #'cdr)
((D . 1) (A . 2) (E . 3) (B . 4) (C . 5))
* zzz
((D . 1) (A . 2) (E . 3) (B . 4) (C . 5))

It works as expected.

* (setf zzz (pairlis '(a b c d e) '(2 4 5 1 3)))
((E . 3) (D . 1) (C . 5) (B . 4) (A . 2))
* (stable-sort zzz #'> :key #'cdr)
((C . 5) (B . 4) (E . 3) (A . 2) (D . 1))
* zzz
((E . 3) (A . 2) (D . 1))

I just do not know why zzz is only 3 members?

Best,
Jinsong

Re: sort behavior is strange

<87bkjia7ve.fsf@wyrd.be>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17384&group=comp.lang.lisp#17384

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 20 Apr 2023 15:28:44 +0000
From: mal@wyrd.be (Lieven Marchand)
Newsgroups: comp.lang.lisp
Subject: Re: sort behavior is strange
References: <u1rhm2$kkfo$1@dont-email.me>
Date: Thu, 20 Apr 2023 17:30:45 +0200
Message-ID: <87bkjia7ve.fsf@wyrd.be>
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:v+Wz6JHi+J8QpkMCEJ/eWdyjkjs=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Lines: 31
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-E6V/kgf0FCELE0rFOLt3HobtW8k/5mg7xkzA0jtOEaxUS57PN2uW05eLGSzfn8R+zPiSPyxDvrEYLBL!2+9q+p3/Q+MaHMa3SvkXP4ZI0lixzUGARFDhbJuNPQ==
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
 by: Lieven Marchand - Thu, 20 Apr 2023 15:30 UTC

Jinsong Zhao <jszhao@yeah.net> writes:

> I hope to sort an associate list with #'sort. I tried something as follows:
>
> * (setf zzz (pairlis '(a b c d e) '(2 4 5 1 3)))
> ((E . 3) (D . 1) (C . 5) (B . 4) (A . 2))
> * (stable-sort zzz #'< :key #'cdr)
> ((D . 1) (A . 2) (E . 3) (B . 4) (C . 5))
> * zzz
> ((D . 1) (A . 2) (E . 3) (B . 4) (C . 5))
>
> It works as expected.
>
> * (setf zzz (pairlis '(a b c d e) '(2 4 5 1 3)))
> ((E . 3) (D . 1) (C . 5) (B . 4) (A . 2))
> * (stable-sort zzz #'> :key #'cdr)
> ((C . 5) (B . 4) (E . 3) (A . 2) (D . 1))
> * zzz
> ((E . 3) (A . 2) (D . 1))
>
> I just do not know why zzz is only 3 members?

sort is destructive so you should assign the result back to the variable
is you want to retain the whole list, like (setf zzz (stable-sort zzz
#'> :key #'cdr)). sort rearranges the cells of its argument and
otherwise zzz keeps pointing to one of the cells in the list. In the
first case you got lucky, in the second case you didn't.

--
Laat hulle almal sterf. Ek is tevrede om die wêreld te sien brand en die vallende
konings te spot. Ek en my aasdier sal loop op die as van die verwoeste aarde.

Re: sort behavior is strange

<69508d77-8e58-44a7-b5e2-954121dfc7cbn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17385&group=comp.lang.lisp#17385

  copy link   Newsgroups: comp.lang.lisp
X-Received: by 2002:a05:620a:13b4:b0:74a:27b5:52c6 with SMTP id m20-20020a05620a13b400b0074a27b552c6mr610433qki.4.1682027149052;
Thu, 20 Apr 2023 14:45:49 -0700 (PDT)
X-Received: by 2002:a05:622a:130f:b0:3ef:3510:7c3f with SMTP id
v15-20020a05622a130f00b003ef35107c3fmr921845qtk.12.1682027148640; Thu, 20 Apr
2023 14:45:48 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.lisp
Date: Thu, 20 Apr 2023 14:45:48 -0700 (PDT)
In-Reply-To: <87bkjia7ve.fsf@wyrd.be>
Injection-Info: google-groups.googlegroups.com; posting-host=2620:0:102f:1005:be4f:a09:1d18:684e;
posting-account=05zmAwoAAAAJZM-3jv1hCWLHGZQceqwA
NNTP-Posting-Host: 2620:0:102f:1005:be4f:a09:1d18:684e
References: <u1rhm2$kkfo$1@dont-email.me> <87bkjia7ve.fsf@wyrd.be>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <69508d77-8e58-44a7-b5e2-954121dfc7cbn@googlegroups.com>
Subject: Re: sort behavior is strange
From: taruss@google.com (Tom Russ)
Injection-Date: Thu, 20 Apr 2023 21:45:49 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2562
 by: Tom Russ - Thu, 20 Apr 2023 21:45 UTC

On Thursday, April 20, 2023 at 8:31:00 AM UTC-7, Lieven Marchand wrote:
> Jinsong Zhao <jsz...@yeah.net> writes:
>
> > I hope to sort an associate list with #'sort. I tried something as follows:
> >
> > * (setf zzz (pairlis '(a b c d e) '(2 4 5 1 3)))
> > ((E . 3) (D . 1) (C . 5) (B . 4) (A . 2))
> > * (stable-sort zzz #'< :key #'cdr)
> > ((D . 1) (A . 2) (E . 3) (B . 4) (C . 5))
> > * zzz
> > ((D . 1) (A . 2) (E . 3) (B . 4) (C . 5))
> >
> > It works as expected.
> >
> > * (setf zzz (pairlis '(a b c d e) '(2 4 5 1 3)))
> > ((E . 3) (D . 1) (C . 5) (B . 4) (A . 2))
> > * (stable-sort zzz #'> :key #'cdr)
> > ((C . 5) (B . 4) (E . 3) (A . 2) (D . 1))
> > * zzz
> > ((E . 3) (A . 2) (D . 1))
> >
> > I just do not know why zzz is only 3 members?
> sort is destructive so you should assign the result back to the variable
> is you want to retain the whole list, like (setf zzz (stable-sort zzz
> #'> :key #'cdr)). sort rearranges the cells of its argument and
> otherwise zzz keeps pointing to one of the cells in the list. In the
> first case you got lucky, in the second case you didn't.

And if you don't want to alter the input list, you would need to call COPY-LIST
or COPY-TREE on it, depending on how much sharing of cons cells you want
between the lists.

Re: sort behavior is strange

<20230421023138.739@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17386&group=comp.lang.lisp#17386

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.lisp
Subject: Re: sort behavior is strange
Date: Fri, 21 Apr 2023 09:33:07 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <20230421023138.739@kylheku.com>
References: <u1rhm2$kkfo$1@dont-email.me> <87bkjia7ve.fsf@wyrd.be>
Injection-Date: Fri, 21 Apr 2023 09:33:07 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5742278e8c762d1f21a82b728d76f1a6";
logging-data="1132245"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/pgGmcnMxj/3JJ8lFy+aSl7aNkbOvVkh0="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:cNTlflHWXzfmGh+0ii7MuPyOfxc=
 by: Kaz Kylheku - Fri, 21 Apr 2023 09:33 UTC

On 2023-04-20, Lieven Marchand <mal@wyrd.be> wrote:
> Jinsong Zhao <jszhao@yeah.net> writes:
>
>> I hope to sort an associate list with #'sort. I tried something as follows:
>>
>> * (setf zzz (pairlis '(a b c d e) '(2 4 5 1 3)))
>> ((E . 3) (D . 1) (C . 5) (B . 4) (A . 2))
>> * (stable-sort zzz #'< :key #'cdr)
>> ((D . 1) (A . 2) (E . 3) (B . 4) (C . 5))
>> * zzz
>> ((D . 1) (A . 2) (E . 3) (B . 4) (C . 5))
>>
>> It works as expected.
>>
>> * (setf zzz (pairlis '(a b c d e) '(2 4 5 1 3)))
>> ((E . 3) (D . 1) (C . 5) (B . 4) (A . 2))
>> * (stable-sort zzz #'> :key #'cdr)
>> ((C . 5) (B . 4) (E . 3) (A . 2) (D . 1))
>> * zzz
>> ((E . 3) (A . 2) (D . 1))
>>
>> I just do not know why zzz is only 3 members?
>
> sort is destructive so you should assign the result back to the variable

That is not the correct reason. In fact only a destructive sort could
possibly achieve the requirement of "I want to sort a list which comes
from a variable, such that the list in the variable appears sorted,
without assigning to the variable".

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: sort behavior is strange

<20230421023312.778@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17387&group=comp.lang.lisp#17387

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.lisp
Subject: Re: sort behavior is strange
Date: Fri, 21 Apr 2023 09:40:15 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <20230421023312.778@kylheku.com>
References: <u1rhm2$kkfo$1@dont-email.me>
Injection-Date: Fri, 21 Apr 2023 09:40:15 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5742278e8c762d1f21a82b728d76f1a6";
logging-data="1132245"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+kO1R0jdv7aDTcTFUOLbdjHtLSPnYdNeg="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:68UGi1DnWnqp6Rz6qajGtSkSNvs=
 by: Kaz Kylheku - Fri, 21 Apr 2023 09:40 UTC

On 2023-04-20, Jinsong Zhao <jszhao@yeah.net> wrote:
> Hi there,
>
> I hope to sort an associate list with #'sort. I tried something as follows:
>
> * (setf zzz (pairlis '(a b c d e) '(2 4 5 1 3)))
> ((E . 3) (D . 1) (C . 5) (B . 4) (A . 2))
> * (stable-sort zzz #'< :key #'cdr)
> ((D . 1) (A . 2) (E . 3) (B . 4) (C . 5))
> * zzz
> ((D . 1) (A . 2) (E . 3) (B . 4) (C . 5))
>
> It works as expected.
>
> * (setf zzz (pairlis '(a b c d e) '(2 4 5 1 3)))
> ((E . 3) (D . 1) (C . 5) (B . 4) (A . 2))
> * (stable-sort zzz #'> :key #'cdr)
> ((C . 5) (B . 4) (E . 3) (A . 2) (D . 1))
> * zzz
> ((E . 3) (A . 2) (D . 1))
>
> I just do not know why zzz is only 3 members?

Sort is destructive, which is why you are seeing any change in the value
of the variable. You are expecting that; i.e. you expect sort to change
the list in place. Just not the surprising results of seeing just part
of the list.

Sort can work in three possible ways (or any mixture thereof):

1. It can keep the list structure exactly the same, and move the values
among the list's cells. If that is the case, you will see the
naively expected behavior that the value of the variable will hold
a sorted list.

2. It can keep elements in their respective cells but rearrange the
cells into a different shape.

3. It can allocate new cells.

Only if a sort implementation strictly followes destructive approach
(1), would you get the expected behavior that the variable turns into
the sorted list.

Because (2) and (3) are allowed, you must capture the return value of
sort (and store it into the variable). Only the return value is
required to be the sorted list. The original head cons cell passed into
the function is not required to be the head of the sorted list,
and, additionally, may be altered.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor