[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: 2016-05-23 Emacs News
From: |
Nicolas Richard |
Subject: |
Re: 2016-05-23 Emacs News |
Date: |
Fri, 10 Jun 2016 12:30:23 +0200 |
User-agent: |
mu4e 0.9.17; emacs 25.0.92.1 |
Rolf Ade <address@hidden> writes:
> (That is:
> http://mbork.pl/2016-05-23_Literal_values_and_destructive_functions)
>
> Wait, what?
>
> Sure, sort is documented as modifying the LIST in place. And the setq
> makes `foo` global, so ... maybe no surprise, or so. But if I eval
>
> (defun destructive-havoc ()
> "Example of destructive havoc."
> (let ((foo '(1 3 2)))
> (message "before sort, foo is: %s" foo)
> (sort foo #'<)
> (message "after sort, foo is: %s" foo)))
>
> and M-: (destructive-havoc) two times, I still get
>
> before sort, foo is: (1 3 2)
> after sort, foo is: (1 2 3)
> before sort, foo is: (1 2 3)
> after sort, foo is: (1 2 3)
>
> in *Messages*. Could someone please explain that to me?
The article you're referring to explains just that. Is it somehow
unclear ? Quoting the article:
| What’s going on?
|
| Well, the literal in the function definition was actually changed. (If
| you evaluate the defun form now, it will be redefined once again to
| the “correct” value.) If you don’t believe it, try this: M-:
| (symbol-function #'destructive-havoc), or even better, M-x
| pp-eval-expression RET (symbol-function #'destructive-havoc) RET and
| see for yourself.
Btw it's even more impressive (imho) if you start from '(3 2 1) :
(defun destructive-havoc ()
"Example of destructive havoc."
(let ((foo '(3 2 1)))
(message "before sort, foo is: %s" foo)
(sort foo #'<)
(message "after sort, foo is: %s" foo)))
calling it twice gives :
before sort, foo is: (3 2 1)
after sort, foo is: (3)
before sort, foo is: (3)
after sort, foo is: (3)
--
Nicolas