Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Common Lisp what is a printed representation?

I'm using SBCL, emacs, slime, and quicklisp to install various packages.

I instantiate and start a hunchentoot acceptor like so,

CL-USER> (hunchentoot:start (make-instance 'hunchentoot:acceptor :port 4242))
#<HUNCHENTOOT:ACCEPTOR (host *, port 4242)>

I test by opening the browser on port 4242 and it works fine.

Then to stop it, I can copy the printed representation and issue the command, like so:

CL-USER> (hunchentoot:stop #<HUNCHENTOOT:ACCEPTOR (host *, port 4242)>)
#<HUNCHENTOOT:ACCEPTOR (host *, port 4242)>

This only works with the printed representation returned by the corresponding start.

This is surprising to me. I thought that the printed representation was simply text returned, presumably because the object itself could not be shown. As such, I thought it was pretty neat that hunchentoot:stop could use the text string to find the object. But then with more experimentation, I noticed that I had to use the printed representation corresponding to the start, not just any one. I also notice that when I put my mouse over the printed representation it highlights the entire segment. So it's not text at all but the object that is actually in the REPL, and I can use it.

So on the one hand what's returned is a print representation so I can see it, but on the other it's the actual object that I can copy and paste in the REPL. Is this right? I guess it must be because I'm doing it. This is totally amazing to me.

Any explanation or insight would be greatly appreciated.

like image 311
kes Avatar asked Apr 02 '13 23:04

kes


People also ask

How do I print a new line in LISP?

Use the TERPRI function (the name stands for "terminate printing", as it's intended to be used to terminate a line of output). You could also use FRESH-LINE . This prints a newline unless you're already at the start of a line.

What are symbols in LISP?

In LISP, a symbol is a name that represents data objects and interestingly it is also a data object. What makes symbols special is that they have a component called the property list, or plist.

What does Princ mean in LISP?

Prints an expression to the command line, or writes an expression to an open file. (princ [expr [file-desc]]) This function is the same as prin1 , except control characters in expr are printed without expansion.


2 Answers

This is SLIME "magic". In fact, you can't do anything with such "print representation" in Lisp, because it is considered an unreadable object: try it in the console mode of your implementation. But SLIME remembers the association to the actual object, so it will substitute the object for that #<...> thing - kind of like a Lisp Machine...

What will work correctly and always is this:

CL-USER> (defvar *server* (hunchentoot:start (make-instance 'hunchentoot:easy-acceptor :port 4242)))
*server*
CL-USER> (hunchentoot:stop *server*)
#<HUNCHENTOOT:ACCEPTOR (host *, port 4242)>
like image 185
Vsevolod Dyomkin Avatar answered Oct 14 '22 07:10

Vsevolod Dyomkin


These are so-called 'Presentations'. See the SLIME User Manual, Presentations.

The documentation also explains what happens if the objects don't go away...

The idea mostly comes from BBN (Zdybel, et al., An Information Presentation System, 1981), then the Symbolics Lisp Machine and the portable Common Lisp Interface Manager, which records the underlying objects during output in its window system. These presentations there work for graphical and textual output and are an important basis for its user interface.

SLIME has picked up the idea for its REPL.

like image 6
Rainer Joswig Avatar answered Oct 14 '22 05:10

Rainer Joswig