I would like to create a nil function that takes any number of symbols and sets them all to nil.
(defun clean (as many args as given by user)
(setq each-arg nil)
)
(clean x y z)
How to do this 'cleanly'?
I would like to create a nil function that takes any number of symbols and sets them all to nil.
(defun clean (as many args as given by user)
(setq each-arg nil)
)
(clean x y z)
How to do this 'cleanly'?
Since you're not quoting the arguments, it has to be a macro:
(defmacro clean (&rest symbols)
`(progn
,@(mapcar (lambda (sym) (list 'setq sym 'nil))
symbols)))
Similar idea as Dmitry, but generates slightly less code:
(defmacro clean (&rest variables)
`(setq ,@(loop for var in variables nconc (list var nil))))
(macroexpand '(clean a b c d))
;; (setq a nil b nil c nil d nil)
Regarding your other questions:
simple, but time consuming way to know is to move the point to the function that you don't know and C-h f or M-xdescribe-function
this will put the function name in the prompt (if it is indeed an Emacs Lisp function) and show the description and, if availably the location in the source code.
I'll try to explain, but I'm no language reference :)
defmacro
- is similar to function, but it doesn't evaluate arguments. Macros are executed when your code is read and compiled into the bytecode. Their primary goal is to generate other code.lambda
- is a macro that creates an anonymous function and returns it.mapcar
- is a high-order function that applies a function to all elements of the list in succession and collects the result into a list in the order it applied the function.&rest
- is a special keyword in the function's lambda-list (i.e. the definition of parameters) which means literally that the identifier following this symbol is a list of all arguments on the right of it).,@
is a special operator used in macros, inside back-quote macros. It instructs the reader that the expression following it must be evaluated, treated as list, and all of its conses must be appended to the form that is being currently parsed.