Are variables and characters different in Lisp?

numberp is a predicate in Lisp, and (numberp 1) returns T as expected. But if I just type numberp in the console, it asks for the variable name undefined.

What is the difference between these 2?

+4
source share
3 answers

The question is already a little wrong.

We are talking about different things in Common Lisp:

  • symbol : data structure in Lisp. A symbol is a data object with a name, value, function, package, and possibly more.

In Common Lisp, a character can have both a value and a function (or macro).

  • a variable is an identifier for a value in Lisp code

There are top-level variables defined by DEFVAR and DEFPARAMETER. There are also local variables defined by LAMBDA, DEFUN, LET, LET * and others.

 (defun foo (i-am-a-variable) ...) (defparameter *i-am-a-global-variable* ...) 
  • A named function is the identifier of a function in Lisp code. Named functions are entered at the top level using DEFUN, DEFGENERIC and DEFMETHOD. There are also local named functions defined by FLET and LABELS.

Example:

 (defun i-am-a-function (foo) ...) (flet ((i-am-a-function (foo) ...)) ...) 

For further functions of the complication function, the names and names of variables are symbols in the source code.

Example:

 (type-of (second '(defun foo () nil))) --> SYMBOL 

Look at the functions and variables:

 (defun foo () (let ((some-name 100)) (flet ((some-name (bar) (+ bar 200))) (+ some-name (some-name some-name))))) 

The above code uses a variable and function with the same character in the source code. Since functions and variables have their own namespace, no conflict arises.

 (+ some-name (some-name some-name)) 

Above, that means we add the variable to the result of the function call for the variable.

This has a practical effect that you can do something like this:

 (defun parent (person) ...) (defun do-something (parent) (parent parent)) 

You need not fear that your local variable names will obscure the global (or local) function. They just reside in different namespaces.

There is only one namespace in the schema, and we must write

 (define (foo lst) (list 'a lst 'n)) 

where in Common Lisp we can write:

 (defun foo (list) (list 'a list 'n)) 

In Common Lisp, there is no need to write lst instead of list - because there is no collision between the local variable list and the global function list .

Access to another namespace

To get the function object stored in a variable, you can use FUNCTION .

 (let ((my-function (function numberp))) (funcall my-function 10)) 

(function foo) can be written shorter as #'foo .

FUNCALL calls the function object.

OTOH, if you want to save a function object from a variable in the function namespace, there is only one way:

 (setf (symbol-function 'foo) my-function) 

It is also necessary that the object is really a function, and not something else (a number, a string, ...). Otherwise, you will see an error message.

A side effect of this is that Common Lisp should never check if FOO is really a function in (foo bar) . There is no way that it can be different from a function or undefined.

+11
source

Functions and variables live in different namespaces in Common Lisp.

Therefore, when you use the name at the position where the function is expected (i.e., at the head of the list that is being evaluated), it searches for a function (or macro) with that name. If you use the same name in the position where a variable is expected, it looks for a variable with that name.

In your case, there is a function called numberp, but not a variable called numberp, so the second case causes an error.

+8
source

One of the differences between Schema (one Lisp dialect, so to speak) and Common Lisp is that there is one namespace for variables and functions in the Schema, while in CL there are two separate namespaces. Thus, in the define schema, it sets this unidirectional associative attribute name-to-value, while in CL there is a definition for value and defun for association of functions.

So in CL you can:

 (define foo ...something...) (defun foo ...somethingElse...) 

to confuse the reader.

There is only one thing in the scheme:

 (define foo something) 

If it is good or bad, it was an almost religious debate in the past ...

0
source

All Articles