[plt-scheme] Destructive Sort. Really dumb question.

From: Robby Findler (robby at cs.uchicago.edu)
Date: Thu May 22 13:18:34 EDT 2008

sort! just changes the structure of the internal cons cell nodes in
your list. It doesn't change what `y' is bound to. After the cons
cells have been rearranged, the variable "y" still refers to that same
cons cell it referred to before, but now that cons cell is somewhere
in the middle of the list, its no longer at the front.

That's why sort! returns something -- so you know which one is now the
front most one.

Robby

On Thu, May 22, 2008 at 12:15 PM, bullockbefriending bard
<kinch1967 at gmail.com> wrote:
> (require (lib "list.ss"))
>
> (let ((y (list 9 8 7 1 11)))
>    (sort y <))
>
> => (1 7 8 9 11)
>
> and all is well with the world.
>
> (let ((y (list 9 8 7 1 11)))
>    (sort! y <))
>
> => (1 7 8 9 11)
>
> ditto.
>
> (let ((y (list 9 8 7 1 11)))
>    (sort! y <)
>    y)
>
> => (8 9 11)
>
> major screen brain impedance mismatch here.
>
> I guess I must be misunderstanding something. If sort! is an in-place
> sort, why don't I see (1 7 8 9 11) ?
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>


Posted on the users mailing list.