What does |5E| mean in Common Lisp? - lisp

I got following error message in Common Lisp.
What does || mean in CL?
CL-USER> (write-to-string 5e)
The variable |5E| is unbound.
[Condition of type UNBOUND-VARIABLE]

|foo| is just a printed representation for symbols. 5e does not read as a number by default, so it is a symbol and may be printed as |5E|. One can use it also to have all kinds of characters in symbols, including whitespace. |this is a symbol, isn't it?| - it is!
CL-USER > (describe '|this is a symbol, isn't it?|)
|this is a symbol, isn't it?| is a SYMBOL
NAME "this is a symbol, isn't it?"
VALUE #<unbound value>
FUNCTION #<unbound function>
PLIST NIL
PACKAGE #<The COMMON-LISP-USER package, 798/1024 internal, 0/4 external>
Note also that Common Lisp uses uppercase symbols by default. Symbols read will be uppercased. So the symbol foo is read and then has a symbol name "FOO". To denote a symbol with lowercase or mixed case letters, one can use |foo|. If you create a lowercase symbol with something like (intern "foo"), then it also will be printed as |foo|. If you create an uppcase named symbol with something like (intern "FOO"), then it will be printed as foo. That's the reason why 5e prints as |5E| with an uppercase E.
If you have a symbol, you can get its name as a string with the function SYMBOL-NAME.
You can read an integer from a string with he function PARSE-INTEGER. It has a keyword parameter :RADIX, where you can provide the radix for reading.
CL-USER > (parse-integer (symbol-name '5e) :radix 16)
94
Otherwise use hex numbers like #x5e or change the read base.
Frank Shearar points out the documentation in the Common Lisp HyperSpec: 2.3.4 Symbols as Tokens.

It's using those characters as quotes. It is trying to interpret 5e as a name of a variable. My guess is that you really want it to interpret it as a hex number, so it should probably be #x5e.

Related

In Lisp, why do we need to use a list function to return a list?

I would have thought this would work:
(defun list-of-things (thing1 thing2 thing3)
"returns a ???"
'(thing1 thing2 thing3))
But this is actually what is needed to return a list:
(defun list-of-things (thing1 thing2 thing3)
"returns a list"
(list thing1 thing2 thing3))
Those are two different things, and a central concept in Lisp languages.
The syntax '(a b c) is a shorthand for (quote (a b c)) and quote is a special form that returns its argument unevaluated. In that case, it would be list, containing three symbols.
On the other hand, list is a normal function, it evaluates its arguments and return the list of their values.
To go a bit more in-depth: before evaluating any expression, the code has to be read. The part that is very specific to Lisp (and, in fact, even more so with Common Lisp) is that the "parser" is actually just a Lisp function, returning Lisp objects. Those objects are then given to the "evaluator", which evaluates them:
When reading the characters '(thing1 thing2 thing3), the reader knows that ' is a reader-macro, and so it reads (quote (thing1 thing2 thing3)). This is a list of two elements, a symbol and another list of three symbols. This list is then given to the evaluator: it knows that quote is a special form that returns its argument unevaluated, and so it simply returns the list (thing1 thing2 thing3), given to it by the reader.
On the other hand, when reading (list thing1 thing2 thing3), the reader also reads this as a list (this time, containing 4 symbols) that it then gives to the evaluator. Now, the evaluator sees that the first symbol is list, a function, and so it evaluates the arguments (i.e. it determines what the symbols thing1 ... are bound to), passes them to the list functions, etc.
All this is possible because Lisp code is defined in terms of Lisp objects (such as lists, etc). After the "parsing" (actually called reading) phase, the evaluator is really given an actual Lisp object to evaluate. Things are of course slightly more complicated when talking about compilation, etc, but the general idea is the same. The quote operator, abbreviated ', is a way to "directly" access the thing created by the reader, and to "bypass" evaluation.
Lisp evaluation rules
You need to understand the evaluation rules of Common Lisp first:
the symbol a is a variable and evaluates to its value
the list (operator ... ) is a form. There are four types of forms: function call, macro form, special form, lambda form
everything else evaluates to itself: numbers, strings, arrays, hash tables, characters, CLOS objects, structures, ...
There is a fixed number of special operators. One is QUOTE. (QUOTE ...) means that the object inside does not get evaluated. '(...) is another notation for (QUOTE ...) - it's easier to write.
So symbols and lists have a role: they are variables and forms.
What if you want to have literal symbols and literal lists? Answer: you have to quote them.
Remember: If you quote a list, then the whole list including its elements is literal data and not evaluated.
Examples:
A global variable defined:
CL-USER 18 > (defvar *a* 42)
*A*
The symbol *a* is quoted and thus the form (QUOTE *A*) evaluates to the symbol:
CL-USER 19 > '*a*
*A*
Inside quoted lists everything is literal data and not evaluated.
CL-USER 20 > '(*a*) ; same as (QUOTE (*A*))
(*A*)
The unquoted symbol is evaluated as a variable:
CL-USER 21 > *a*
42
A function call has all its arguments evaluated:
CL-USER 22 > (list *a*)
(42)
A backquote expression allows certain elements in a list to be evaluated using the comma prefix:
CL-USER 23 > `(41 ,*a* (+ 1 *a*) (+ 1 ,*a*) ,(+ 2 *a*))
(41 42 (+ 1 *A*) (+ 1 42) 44)
Summary
When you want to return a list from a function, you can:
return a fixed literal list
or compute a new list based on some objects by constructing the list with the usual operators: CONS, LIST, APPEND, ...
or use a backquote expression which gets expanded by the Lisp reader into an implementation specific version of 2.
The list function is used for creating lists. They called LISP because it’s for LISt Processing. Also, because Lisp functions are written as lists, they can be processed exactly like list.

Common Lisp equality on symbol in web application

The following function:
(defun check-for-arrow (x)
(format t "**~s**~s**~s**~s**"
(length (string x))
x
(eq x '->)
(and (eq (elt (string x) 0) #\-)
(eq (elt (string x) 1) #\>))) ; for debug
(eq x '->))
when called from REPL, with:
(check-for-arrow '->)
prints, with tracing:
0> Calling (CHECK-FOR-ARROW ->)
**2**->**T**T**
<0 CHECK-FOR-ARROW returned T
Instead, when called inside a Hunchentoot web application, over data read in a form, when called over the symbol '->', prints:
0> Calling (NORMALIZER::CHECK-FOR-ARROW ->)
**2**->**NIL**T**
<0 NORMALIZER::CHECK-FOR-ARROW returned NIL
The Lisp is Clozure Common Lisp.
Does this depend on a different way of interning symbols ? It is possible to use 'eq' on symbols or I have to transform the arrow in a string and check for string equality?
Thanks.
Common Lisp has packages. Packages are kind of namespaces for symbols.
Thus one can have many different symbols named "->", each one in a different package.
Thus normalizer::-> is not necessarily EQ to cl-user::->.
Symbols can also be NOT interned in a package, thus one can have many different symbols of the same name and no a package.
CL-USER 2 > '#:->
#:->
CL-USER 3 > (describe *)
#:-> is a SYMBOL
NAME "->"
VALUE #<unbound value>
FUNCTION #<unbound function>
PLIST NIL
PACKAGE NIL
CL-USER 4 > '->
->
CL-USER 5 > (describe *)
-> is a SYMBOL
NAME "->"
VALUE #<unbound value>
FUNCTION #<unbound function>
PLIST NIL
A typical problem:
One has a function which tests for EQ of something with a certain symbol FOO. The user inputs FOO.
But how does your Lisp function convert the user input into a symbol? In which package will the symbol be? Remember symbols differ by name AND package. If you don't specify the package, the default is the value of the variable CL:*PACKAGE*. But this variable can have different values at different times.
Just extend your test function to print the packages of the symbols and you will see the difference.
CL-USER 7 > (package-name (symbol-package '->))
"COMMON-LISP-USER"

How to acquire unique object id in Emacs Lisp?

Does emacs lisp have a function that provides a unique object identifier, such as e.g. a memory address? Python has id(), which returns an integer guaranteed to be unique among presently existing objects. What about elisp?
The only reason I know for wanting a function like id() is to compare objects, and ensure that they only compare equal if they are the same (as in, in the same memory location). In Lisps, this is done a bit differently from in Python:
In most lisps, including elisp, there are several different notions of equality. The most expensive, and weakest equivalence is equal. This is not what you want, since two lists (say) are equal if they have the same elements (tested recursively with equal). As such
(equal (list 1 2) (list 1 2)) => T
is true. At the other end of the spectrum is eq, which tests "identity" rather than equality:
(eq (list 1 2) (list 1 2)) => NIL
This is what you want, I think.
So, it seems that Python works by providing one equality test, and then a function that gives you a memory location for each object, which then can be compared as integers. In Elisp (and at least Common Lisp too), on the other hand, there is more than one meaning of "equality".
Note, there is also "eql", which lies somewhere between the two.
(EDIT: My original answer probably wasn't clear enough about why the distinction between eq and equal probably solves the problem the original poster was having)
There is no such feature in Emacs Lisp, as far as I know. If you only need equality, use eq, which performs a pointer comparison behind the scenes.
If you need a printable unique identifier, use gensym from the cl package.
If you need a unique identifier to serve as an index in a data structure, use gensym (or maintain your own unique id — gensym is simpler and less error-prone).
Some languages bake a unique id into every object, but this has a cost: either every object needs extra memory to store the id, or the id is derived from the address of the object, which precludes modifying the address. Python chooses to pay the cost, Emacs chooses not to.
My whole point in asking the question was that I was looking for a way to distinguish between the printed representations of different symbols that have the same name. Thanks to the elisp manual, I've discovered the variable print-gensym, which, when non-nil, causes #: to be prepended to uninterned symbols printed. Moreover, if the same call to print prints the same uninterned symbol more than once, it will mark the first one with #N= and subsequent ones with `#N#. This is exactly the kind of functionality I was looking for. For example:
(setq print-gensym t)
==> t
(make-symbol "foo")
==> #:foo
(setq a (make-symbol "foo"))
==> #:foo
(cons a a)
==> (#1=#:foo . #1#)
(setq b (make-symbol "foo"))
==> #:foo
(cons a b)
==> (#:foo . #:foo)
The #: notation works for read as well:
(setq a '#:foo)
==> #:foo
(symbol-name a)
==> "foo"
Note the ' on '#:foo--the #: notation is a symbol-literal. Without the ', the uninterned symbol is evaluated:
(symbol-name '#:foo)
==> "foo"
(symbol-name #:foo)
==> (void-variable #:foo)

NIL representation in PC Scheme TI implementation

I am learning LISP using PC Scheme TI implementation and from the book 'Structure and Interpretation of Computer Programs'. PC scheme doesn't seem to have a 'nil' variable
[VM ERROR encountered!] Variable not defined in current environment
NIL
Instead '()' seems to work.
[55] (define one-through-four (list 1 2 3 4 ()))
ONE-THROUGH-FOUR
Do both have the same meaning? What is the correct way to use a sequence of no elements in the PC scheme dialect?
Thanks.
When you say that this dialect "does not have a NIL variable", you are right in two ways. Not only does not nil exist, but it seems to be treated as an ordinary symbol that you can use as a variable. It says "Variable not defined".
In Lisp dialects where nil has a special meaning, nil usually cannot be used as a variable.
nil is equivalent to () only in "classical" Lisps (my term). By this I mean Lisps that are related by ancestry or imitation to the original John MacCarthy Lisp. Emacs Lisp and Common Lisp are that way. The classical Lisps use nil as the list terminator: i.e. (cons 1 nil) producing the list (1)
Scheme is not a classical Lisp in this sense. There is no nil. It uses () as the printed notation for an empty list and when you want that value in an expression, it is quoted, just like a non-empty list literal: '(). In Scheme, (cons 1 nil) is written (cons 1 '()). Boolean false is represented by an object which is notated #f. An empty list is true not false, so (if '() 1 2) yields 1, not 2 like in a classical Lisp. Neither '() nor #f are symbols.
Scheme is also case-sensitive. Even if you do have a variable NIL in Scheme, it has nothing to do with nil. You can use both as variable names.
The SICP text causes confusion on this topic by, in its early chapters, making references to a variable called nil which holds the value of '(). There is no such variable in Scheme; you have to make it yourself if you want those examples to work. This is an unfortunate aspect of the textbook.
If you define a variable nil whose value is the object '(), so that that Chapter 2 code from SICP works, it is still not equivalent to an empty list the way it is in a classical Lisp, because in a classical Lisp, nil and () are equivalent notations for a symbol. The symbol nil itself is the empty list, and vice versa. It is not the name of a variable which merely holds a list. And so the quote expression 'nil also evaluates to the empty list! Whereas in scheme 'nil evaluates to the ordinary symbol nil even if you've defined it as a variable holding the empty list.
Regarding the second question, "() seems to work, do both have the same meaning?". The "same meaning" aspect is covered above: no, not the same meaning in Scheme. As far as it working, consider the following quote from the R5RS Scheme Report:
Note: In many dialects of Lisp, the empty combination, (), is a legitimate expression. In Scheme, combinations must have at least one subexpression, so () is not a syntactically valid expression. [4.1.3 Procedure Calls]
Therefore this loose treatment of () as valid expression that produces the empty list appears to be a PC Scheme extension of behavior, not standard Scheme. It is accepting an expression which is not a syntactically valid Scheme expression.

Am I missing some important fact about symbols in LISP?

What is the correct way to do this?
(defparameter form1 (list 'baz "hello"))
(setf (car form1) (intern "print"))
(eval form1)
What is the significance of || (intern) uses?
What is the significance of #:|| (make-symbol) uses?
If they are just part of the name, what is the rationale of creating a different symbol from that which I have specified?
Update:
(intern "PRINT")
works
The vertical bars are the quotes for symbols. Symbols that you've entered literally in the source code don't use them because the reader turns them into all-caps, and all-caps symbols don't need to be quoted.
The #: at the front signifies uninterned symbols, i.e. symbols that don't belong to any package. intern puts symbols into the current package. Only symbols of the current package are printed without the name of their package.