Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common-Lisp: binding formal parameters, exactly what is passed?

Suppose we have a symbol, with a symbol value and a function value and a property list and let us call it q. Suppose also that we have a function f with formal parameter v, e.g. (f (v) ... ) and call the function like (f q).

My question is: what is exactly passed to v? Are

  1. the value of q;
  2. the function value of q;
  3. property list of q,

passed to the formal parameter v?

If they are all passed to v, then I am puzzled by the fact that we really need the functions funcall and apply. If v would really have both the value and the function value, then it can surely itself decide that when we write (v 3), then it must use the function value of v instead of (funcall v 3). And when we use (setq v 3) then it must use the value of v.

What is exactly passed to v and why v is not a symbol, but just a "parameter" or "variable", is an enigma to me. But I believe that it was in Lisp 1.5 really a symbol. But in common Lisp, there seems to be some room to confusion.

like image 604
Dirk Bollaerts Avatar asked Feb 08 '23 00:02

Dirk Bollaerts


1 Answers

If you have

(f q)

it means call the function f with the value of q.

  1. Lisp sees that f is a function, so the whole (f q) is a function form.
  2. Lisp evaluates q to its value.
  3. Lisp calls f with one value.
  4. Lisp binds the local variable v to the passed value
  5. Lisp executes the body of the function f ...

v is in the source code a symbol, but it denotes a variable. In compiled code the symbol is gone. Since Common Lisp uses lexical bindings, variables are now lexical references.

like image 54
Rainer Joswig Avatar answered Apr 28 '23 10:04

Rainer Joswig