Juanito Fatas
Juanito Fatas

Reputation: 9969

why every common lisp returns what's being defined

I'm wondering why every common lisp function/macro definitions returns what's being defined? It could return nil or the function itself. Why return the defined symbol? Is there anything I could do about it?

> (defun foo ()) => foo

> (defmacro bar ()) => bar

Upvotes: 3

Views: 159

Answers (2)

Sylwester
Sylwester

Reputation: 48775

The reason every form returns something is how it's made by design. In a read-eval-print-loop this is useful so you get a confirmation even when it's not used.

What is returned is not important other than CL have it specified in its specification so that every CL is doing the same.

Remember it's only in the REPL it displays whats returned. When you run a script, only things you print will display.

The other main dialect, Scheme, is said not to return anything when it mutates.. How it really is is that implementations return a special object that only the REPL ignore. If you (display (set! X 6)) you will get something printed.

Upvotes: 2

Rainer Joswig
Rainer Joswig

Reputation: 139411

I would expect that every defining form in Lisp either returns the name of what has been defined (like in DEFUN) or the object that has been defined (like in DEFCLASS).

That's a useful value which later can be used. In a Lisp interaction, the variable *, ** and *** have the last values. Thus you can do in a Lisp with interpreter and compiler:

CLISP:

[1]> (defun foo-with-a-long-name (a) (+ a 42))
FOO-WITH-A-LONG-NAME

compiling the function is then just:

[2]> (compile *)
FOO-WITH-A-LONG-NAME ;
NIL ;
NIL

No errors. Let's see the disassembly:

[3]> (disassemble *)

Disassembly of function FOO-WITH-A-LONG-NAME
(CONST 0) = 42
1 required argument
0 optional arguments
No rest parameter
No keyword parameters
4 byte-code instructions:
0     (CONST&PUSH 0)                      ; 42
1     (LOAD&PUSH 2)
2     (CALLSR 2 55)                       ; +
5     (SKIP&RET 2)
NIL

Okay, looks good.

Upvotes: 5

Related Questions