Vinn
Vinn

Reputation: 1181

Using a quote before a symbol inside a function?

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?

Upvotes: 2

Views: 89

Answers (4)

Rainer Joswig
Rainer Joswig

Reputation: 139261

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

Upvotes: 2

coredump
coredump

Reputation: 38809

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.

Upvotes: 1

Martin Půda
Martin Půda

Reputation: 7568

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:

Upvotes: 3

Tomas Zellerin
Tomas Zellerin

Reputation: 426

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.

Upvotes: 2

Related Questions