3 ms·
Looking at a site like this makes me appreciate a language like Clojure more. I was quite surprised a while ago when I learned that `sort` in Common Lisp can m
by hjek 8y ago
Looking at a site like this makes me appreciate a language like Clojure more.
I was quite surprised a while ago when I learned that `sort` in Common Lisp can mutate somehow randomly.
> The sorting operation can be destructive in all cases.
http://clhs.lisp.se/Body/f_sort_.htm http://clhs.lisp.se/Body/f_sort_.htm
- klibertp 8y agoYou'll find people much better suited to explain this on HN, but I'd guess it's about efficiency. CL was defined long ago and one of the goals was for it to be fast. It really is, but there is a pervasive mutability in most of it because of this, with functions like nreverse, rplca, nconc and so on.
- lispm 8y agoAdd something like this to your library and you are fine: (defun isort (sequence predicate &key (key #'identity)) (sort (copy-seq sequence) predicate :key key))
- kbp 8y agoSomewhat relatedly, a very common mistake in Lisp is modifying literals; a lot of people don't realise that there's a difference between writing '(a b c) and writing (list 'a 'b 'c), but QUOTE's description in the spec plainly says "The consequences are undefined if literal objects (including quoted objects) are destructively modified." To demonstrate the difference in practice (DELETE is the destructive version of REMOVE): CL-USER> (defun abc-without (letter) (delete letter '(a b c))) ABC-WITHOUT CL-USER> (abc-without 'a) (B C) CL-USER> (abc-without 'b) (A C) CL-USER> (abc-without 'c) (A) This behaves strangely because it modifies the constant list (A B C) rather than consing up a new one on each invocation (like calling LIST or COPY-SEQ would), so by the end the list it passes to DELETE has been reduced to (A).