2 ms·
You can't break it with LET because functions are in a separate namespace with variables, and because the function lookup done for 'LIST isn't in the lexical sc
by kazinator 4y ago
You can't break it with LET because functions are in a separate namespace with variables, and because the function lookup done for 'LIST isn't in the lexical scope; it's done inside the mapcar function in another scope, or maybe in a lower-level function like funcall or apply.
Because of separate namspaces, you can't break this either:
(let ((list '(1 2 3)))
(mapcar #'list ...))
But the following will break it:
(flet ((list (x) 42))) ;; actually this is undefined behavior: see below
(mapcar #'list ...))
#'list is (function list) which is lexically resolved, and so it refers to the local function flet. If we use 'list, that is unaffected; it goes to the global one.
This is a bad example because redefining the standard list function in any scope is not well defined behavior. Implementations can allow it or diagnose it. Because it's undefined behavior, implementations don't have to worry about breakage of list uses which occur in macro expansions. E.g. if the syntax `(,foo ,bar) happens to compile to (list foo bar), and your redefinition of list has taken effect in that scope, then that syntax has broken. Oops!
- kagevf 4y ago> the function lookup done for 'LIST isn't in the lexical scope; it's done inside the mapcar function in another scope, or maybe in a lower-level function like funcall or apply Ah, OK, I think that's the key part I was missing ... that explanation also accounts for what I saw in the REPL ... thanks again! :)