2

I am a bit confused about when to NOT use the quote before a symbol.

For example if I want to remove a function from my image:

> (fmakunbound 'func-name)
FUNC-NAME

but, if i create a function for doing the same thing, it looks like this:

(defun remove-func (symbol)
  (fmakunbound symbol))

In the second example, why does the fmakunbound in the remove-func function not need the quoted symbol?

Vinn
  • 1,030
  • 5
  • 13

4 Answers4

3

When you call (fmakunbound 'func-name), all arguments are evaluated, so fmakunbound receives the symbol func-name.

When you call (remove-func 'func-name), all arguments are evaluated and a variable named symbol will get the value func-name (this value is a symbol). Then you call (fmakunbound symbol), all arguments are evaluated, so the symbol symbol is evaluated to its value, which is symbol func-name.

In both examples, fmakunbound receives the symbol func-name.

See also:

Martin Půda
  • 7,353
  • 2
  • 6
  • 13
2

Quoted symbols evaluate to the symbol itself. Unquoted symbols evaluate (some special cases aside) to value of a variable named with the symbol.

In the first example, func-name is directly name of the function, so it needs to be passed as a parameter as-is, so quoted.

In the second example, symbol is name of the variable that holds the name of the function to unbound, so it needs to be evaluated to get the actual name (also symbol), so it is not quoted.

Tomas Zellerin
  • 366
  • 1
  • 7
2

You might want to make sure you understand evaluation rules.

> foo    ; -> this is a variable, the result is its value

> 'foo   ; -> this is the expression (QUOTE FOO), the result is the symbol FOO

and

> (sin x)   ; -> calls the function SIN with the value of the variable X

> (sin 'x)  ; -> calls the function SIN with the symbol X.
            ;  ==> this is an ERROR, since SIN expects a number

> (fmakunbound FOO)   ; calls FMAKUNBOUND with the value of the variable FOO
                      ; , whatever it is


> (fmakunbound 'FOO)  ; calls FMAKUNBOUND with the result of evaluating 'FOO
                      ; 'FOO evaluates to the symbol FOO.
                      ; thus FMAKUNBOUND is called with the symbol FOO
                      ; thus FMAKUNBOUND removes the function binding 
                      ; from the symbol FOO
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
1

You cannot add a quote for symbol in remove-func in either places:

(defun remove-func ('symbol)
  (fmakunbound symbol))

After reader macros are applied, this is the same as:

(defun remove-func ((quote symbol))
  (fmakunbound symbol))

defun is a macro, the list of arguments is not evaluated but used literally by the macro; mandatory arguments in ordinary lambda lists cannot be lists, only literal symbols.

(defun remove-func (symbol)
  (fmakunbound 'symbol))

Here the local variable named symbol is never used, and the literal symbol symbol is given as an argument to fmakunbound.

coredump
  • 37,664
  • 5
  • 43
  • 77