2009-02-06

lisp's sort is destructive

http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/606f3c8f0ab8cc53

2008-01-05

I'm a perl expert, programing it in industry daily from 1998 to 2002. I started to learn elisp in 2005 casually. (am a daily emacs user since 1997) To my surprise, for tasks of text processing (and sys admin), elisp's power, ease, and convenience is almost a order above Perl. This is particular painful to realize for me because even being a Perl hater from the very beginning, fully aware of its rampant lies, i was nevertheless strongly deceived by the wide-spread understanding that Perl IS the most suitable language for text processing tasks. (and conversely, due to emacs lisper's lack of emacs lisp advocacy, partly due to the supression by Scheme lisp and Common Lisp factions, emacs's power as a text-processing computer language system is basically unknown to the programers at large (often, emacs lisp is understood to programers who actually heard of lisp, as “just a extension niche language” usually with demeaning connotations))

Besides industrial programing, my personal use of perl is primarily text processing. So, i was quite pissed by the extent of damage perl's lies caused to me on this regard. I'm currently, casually doing all my text processing needs in emacs instead of Perl or Python. (e.g. typically a function that process tens to tens of thousand files.)

Anyway, the main point for this post is something i realized in emacs lisp that's rather bizarre. That is: its “sort” function destroys the variable of list it is sorting!!!

Here's a example:

(setq mylist '("c" "d" "a"))
(sort mylist 'string< )
mylist ; ⇒ ("c" "d")

O my god???

This actually caused me few hours to debug. (among other reasons. e.g. i'm still learning elisp system)

In high level languages, sort will either sort the variable in-place, or return the sorted list. I have not come across any language that actually destroy the variable in the process. I asked a bit, and it seems Common Lisp's sort has the same behavior.

So, my question is: What could possibly be the use of this? When does a programer EVER need to do simply just (sort mylist 'f) ?

Of course, lisp is old, with many quaint qualities (e.g. the cons business; elisp's char being integers; elisp's dynamic scope; Common Lisp's big baggage ...etc.), but what could have possibly made the design of “sort” function the way it is?

Xah
∑ http://xahlee.org/

No comments:

Post a Comment