开发者

Why doesn't a LISP defun require a quote before its parameter argument?

开发者 https://www.devze.com 2023-01-25 14:54 出处:网络
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 atom

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开发者_StackOverflow中文版. 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


IIRC in Common Lisp at least defun is a macro (HyperSpec), meaning it may define any evaluation strategy whatsoever for its arguments.


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".


Your presumption is correct. Defun is commonly a special form or macro


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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消