Reputation: 982
Take this function:
(defun sum-greater (x y z)
(> (+ x y) z))
It's my understanding that in LISP the first element in a list always represents a function to be performed on the subsequent atoms/lists. So why doesn't LISP treat the x
in (x y z)
as a function to be performed on y
and z
. Clearly this would not be desirable behavior, but it would be the expected behavior.
Presumably the function that defines defun
somehow overrides the standard LISP evaluation of a list? If so, could you detail this?
Thanks
Upvotes: 12
Views: 1093
Reputation: 7727
defun is not a function, but a special form (or boils down to one), and for these, evaluation mechanics are different. Similar examples would be if, where one of the arguments is even discarded entirely without being evaluated at all!
Upvotes: 2
Reputation: 139251
You can download here a basic introduction into Lisp:
Common Lisp: A Gentle Introduction to Symbolic Computation, by David S. Touretzky.
Lisp and especially Common Lisp has several Lisp forms:
function calls
macro calls
special forms
DEFUN is a macro. Thus the macro defines which parts are evaluated and which not. For ANSI Common Lisp this is defined in the standard and implemented by the DEFUN macro.
Upvotes: 3
Reputation: 47183
defun
is special because it is a macro. And since macros can be implementation dependent, all sorts of black magic can happen beneath the hood.
Lisp HyperSpec (Common Lisp) says, and I quote: "None of the arguments are evaluated at macro expansion time".
Upvotes: 6
Reputation: 41749
Your presumption is correct. Defun is commonly a special form or macro
Upvotes: 3
Reputation: 4336
IIRC in Common Lisp at least defun
is a macro (HyperSpec), meaning it may define any evaluation strategy whatsoever for its arguments.
Upvotes: 8