[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

does sbcl have a bug in sort, or I do not understand hyperspec

Bigos

10/17/2015 12:59:00 PM

I have created a gist
https://gist.github.com/bigos/fe063f7033...
to illustrate the problem for easy copying and pasting

CL-USER> (defparameter zzz '(a-list-of-50-integers))
ZZZ

CL-USER> (defparameter xxx (sort zzz '<))
XXX

CL-USER> (length zzz)
34

CL-USER> (length xxx)
50

is it expected behaviour?
3 Answers

Carlos

10/17/2015 1:08:00 PM

0

[Bigos <ruby.object@googlemail.com>, 2015-10-17 13:59]
> I have created a gist
> https://gist.github.com/bigos/fe063f7033...
> to illustrate the problem for easy copying and pasting
>
> CL-USER> (defparameter zzz '(a-list-of-50-integers))
> ZZZ
>
> CL-USER> (defparameter xxx (sort zzz '<))
> XXX
>
> CL-USER> (length zzz)
> 34
>
> CL-USER> (length xxx)
> 50
>
> is it expected behaviour?

Sort is a destructive operation, so it can freely destroy its argument.
You can only rely on the return value.

But in this case, you are applying a destructive operation to a literal
list, which has undefined consequences (see 3.7.1), so here there isn't
any expected behaviour at all.

--

Jocelyn Fréchot

10/17/2015 1:14:00 PM

0

On 17/10/2015 14:59, Bigos wrote:

> I have created a gist
> https://gist.github.com/bigos/fe063f7033...
> to illustrate the problem for easy copying and pasting
>
> CL-USER> (defparameter zzz '(a-list-of-50-integers))
> ZZZ
>
> CL-USER> (defparameter xxx (sort zzz '<))
> XXX
>
> CL-USER> (length zzz)
> 34
>
> CL-USER> (length xxx)
> 50
>
> is it expected behaviour?

Yes it is:

â??sort and stable-sort *destructively* sort sequences according
to the order determined by the predicate function.�

Usually you do something like:

(setf zzz (sort zzz #'<))

--
Jocelyn Fréchot

William James

10/17/2015 2:33:00 PM

0

Bigos wrote:

> I have created a gist
> https://gist.github.com/bigos/fe063f7033...
> to illustrate the problem for easy copying and pasting
>
> CL-USER> (defparameter zzz '(a-list-of-50-integers))
> ZZZ
>
> CL-USER> (defparameter xxx (sort zzz '<))
> XXX
>
> CL-USER> (length zzz)
> 34
>
> CL-USER> (length xxx)
> 50
>
> is it expected behaviour?

It's not expected behavior.

Gauche Scheme:

Function: sort seq :optional cmpfn keyfn
Function: sort! seq :optional cmpfn keyfn

[SRFI-95+] Sorts elements in a sequence seq in ascending order
and returns the sorted sequence. sort! destructively reuses
the original sequence.

Note that only sort! is destructive.

gosh> (define xxx '(3 2 0 4 5 9 8 7))
xxx
gosh> (sort xxx)
(0 2 3 4 5 7 8 9)
gosh> xxx
(3 2 0 4 5 9 8 7)
gosh> (define yyy "chattering")
yyy
gosh> (sort yyy)
"aceghinrtt"
gosh> yyy
"chattering"

--
He has nothing but kind sentiments for those who would destroy his home and
family.... He is universally tolerant.... If he has any principles, he keeps
them well concealed.... He is, to the extent of his abilities, exactly like
the next citizen, who, he trusts, is trying to be exactly like him: a faceless,
characterless putty-man. --- Father Feeney; "Should Hate Be Outlawed?"