I have tried to understand the difference between Lisp-1 and Lisp-2 and how this relates to Clojure but I still do not understand properly. Can anyone enlighten me?
According to wikipedia:
Whether a separate namespace for functions is an advantage is a source of contention in the Lisp community. It is usually referred to as the Lisp-1 vs. Lisp-2 debate. Lisp-1 refers to Scheme's model and Lisp-2 refers to Common Lisp's model.
It's basically about whether variables and functions can have the same name without clashing. Clojure is a Lisp-1 meaning that it does not allow the same name to be used for a function and a variable simultaneously.
list
is often used as a function parameter and nobody things, OMG that is so confusing since (list ...)
is a standard function. Many functions that have list
as a variable don't use the list
function, or don't use it near that variable. Even when that does happen it's not too bad: (list foo list)
. This isn't any more confusing than a sentence like "fight the good fight" where the same word appears as a noun and verb. –
Origan You might like to read this paper by Richard Gabriel. It is a summary of the issues that the Lisp community were discussing in Lisp1 vs Lisp2. It's a bit dense and slow moving in the first few sections, but is much easier to read by the time you get past section 5.
Basically, Lisp1 has a single environment that maps symbols to values, and those values can be either "regular" or functions. Lisp2 has (at least) two namespaces (symbols have a slot for their a function value and one for a regular value). So, in Lisp2, you can have a function named foo and a value named foo, whereas in Lisp1, the name foo can refer only to a single value (function or otherwise).
There are several tradeoffs and differences of taste between the two, but read the paper for the details. Christian Queinnec's book, "Lisp in Small Pieces" also has discussion of the differences woven through the text.
funcall
and function
operators. These disappear in a Lisp-1. –
Origan mapcar
and the like, some arcane rules of resolution there. Again IIANM. –
Fernanda According to wikipedia:
Whether a separate namespace for functions is an advantage is a source of contention in the Lisp community. It is usually referred to as the Lisp-1 vs. Lisp-2 debate. Lisp-1 refers to Scheme's model and Lisp-2 refers to Common Lisp's model.
It's basically about whether variables and functions can have the same name without clashing. Clojure is a Lisp-1 meaning that it does not allow the same name to be used for a function and a variable simultaneously.
list
is often used as a function parameter and nobody things, OMG that is so confusing since (list ...)
is a standard function. Many functions that have list
as a variable don't use the list
function, or don't use it near that variable. Even when that does happen it's not too bad: (list foo list)
. This isn't any more confusing than a sentence like "fight the good fight" where the same word appears as a noun and verb. –
Origan In a Lisp-1 a symbol can only have 1 value. In a Lisp-2 a symbol can have two values which are 1) the value when the symbol is interpreted as a function and 2) when the symbol is interpreted as a value.
Down in the implementation in a Lisp-1 the structure that contains the value associated with a symbol has just one slot for a pointer to the data and type. In a Lisp-2 there are two pointers, one the function value, the other the variable value.
In Common Lisp, a Lisp-2, you can call (symbol-function ‘a) (symbol-value ‘a) to get the function and value separately. If ‘a is defined as the add function and (setq a 3)then (a a a 1) would return 7.
In a lisp 1 you couldn’t do that because a could only be one thing, and the context does not change the meaning.
In Common Lisp #’ is an abbreviation for symbol-function. (setf (symbol-function ‘a) #’+ ) would set a to be the function sum.
If you wanted, you could create a function a that would set a to the value last returned by a call to a, perhaps if a was expensive to compute.
In a Lisp-2 if you assigned a function to a symbol then to call that function you use the funcall operator.
© 2022 - 2024 — McMap. All rights reserved.