Anonymous user
Function definition: Difference between revisions
→{{header|Common Lisp}}
Line 195:
Common Lisp has several kinds of functions: ordinary functions, macros and generic functions.
===Ordinary Functions===
Ordinary functions that operate on values:▼
<lang lisp>(defun multiply (a b)
Line 202 ⟶ 204:
(multiply 2 3)</lang>
===Macros===
Macros, are function which operate on the source code forms of the macro call (at compile time, if the code is being compiled), and not on the run-time values of the expressions. They perform a source-to-source transformation.▼
▲Macros, are
Here, we write a macro which implements a C-language style for loop, with an initialization, test and increment at the top.
Line 211 ⟶ 215:
The macro is robust and hygienic, since the labels are macro-generated. It will nest properly, such that (continue) and (break) branch within the correct loop. Of course, the user has to be aware that the (break) and (continue) forms are implicitly defined; in this sense, there is a deliberate lack of hygiene which is part of the semantics.
<lang lisp>(defmacro c-style-for (init-expr cond-expr incr-expr &body body-forms)▼
<lang lisp>▼
▲(defmacro c-style-for (init-expr cond-expr incr-expr &body body-forms)
(let ((top-label (gensym))
(out-label (gensym))
Line 248 ⟶ 251:
2
4
===Generic Functions===
Lisp's generic functions are part of the object system. Generic functions are compiled to ordinary functions, and so are called in the ordinary way. Internally, however, they have the special behavior of dispatching one or more methods based on specializable parameters.
|