Opened 12 years ago

Closed 12 years ago

'sort' function truncates lists

Reported by: Owned by: Brian gb major ANSI CL Compliance unspecific

Description

Welcome to Clozure Common Lisp Version 1.1-r7809 (DarwinPPC32)!

? (let ((x '(1 2 3)))

(sort x #'<) x)

(1 2 3) ? (let ((x '(1 3 2)))

(sort x #'<) x)

(1 2 3) ? (let ((x '(2 1 3)))

(sort x #'<) x)

(2 3) ? (let ((x '(2 3 1)))

(sort x #'<) x)

(2 3) ? (let ((x '(3 1 2)))

(sort x #'<) x)

(3) ? (let ((x '(3 2 1)))

(sort x #'<) x)

(3)

I know that 'sort' is a "destructive" operation, but I think this goes too far.

Note that the problem does not arise when the argument is a vector rather than a list:

? (let ((x '#(3 2 1)))

(sort x #'<) x)

#(1 2 3)

Also, 'stable-sort' exhibits the same behavior.

comment:1 Changed 12 years ago by Brian

Sorry for the lame formatting of the original post. Here's what I hope is a more intelligible version:

Welcome to Clozure Common Lisp Version 1.1-r7809 (DarwinPPC32)!

? (let ((x '(1 2 3)))
(sort x #'<) x)
(1 2 3)

? (let ((x '(1 3 2)))
(sort x #'<) x)
(1 2 3)

? (let ((x '(2 1 3)))
(sort x #'<) x)
(2 3)

? (let ((x '(2 3 1)))
(sort x #'<) x)
(2 3)

? (let ((x '(3 1 2)))
(sort x #'<) x)
(3)

? (let ((x '(3 2 1)))
(sort x #'<) x)
(3)

I know that 'sort' is a "destructive" operation, but I think this goes too far.

Note that the problem does not arise when the argument is a vector rather than a list:

? (let ((x '#(3 2 1)))
(sort x #'<) x)
#(1 2 3)

Also, 'stable-sort' exhibits the same behavior.

comment:2 Changed 12 years ago by gb

• Resolution set to invalid
• Status changed from new to closed

This is absolutely, positively not a bug.

A good explanation of the issues involved can be found in section 3-3 of

There are also issues related to using destructive operations (like SORT) on quoted constants. Consider:

? (defun maybe-sort-constant-list (sort-it)
(let* ((x '(3 2 1)))
(when sort-it (sort x #'<))
x))
MAYBE-SORT-CONSTANT-LIST
? (maybe-sort-constant-list t)
(3)
? (maybe-sort-constant-list nil)
(3)

It is worth trying to understand what's happening here.

Note: See TracTickets for help on using tickets.