Tutorial by Examples

TODO: Maybe move the explanations to remarks and add examples separately FOOF In Common Lisp, there is a concept of Generalized References. They allow a programmer to setf values to various "places" as if they were variables. Macros that make use of this ability often have a F-postfix in...
An Anaphoric Macro is a macro that introduces a variable (often IT) that captures the result of a user-supplied form. A common example is the Anaphoric If, which is like a regular IF, but also defines the variable IT to refer to the result of the test-form. (defmacro aif (test-form then-form &o...
Macro expansion is the process of turning macros into actual code. This usually happens as part of the compilation process. The compiler will expand all macro forms before actually compiling code. Macro expansion also happens during interpretation of Lisp code. One can call MACROEXPAND manually to ...
Macros return code. Since code in Lisp consists of lists, one can use the regular list manipulation functions to generate it. ;; A pointless macro (defmacro echo (form) (list 'progn (list 'format t "Form: ~a~%" (list 'quote form)) form)) This is often very hard to...
The expansion of a macro often needs to use symbols that weren't passed as arguments by the user (as names for local variables, for example). One must make sure that such symbols cannot conflict with a symbol that the user is using in the surrounding code. This is usually achieved by using GENSYM, ...
These macros merge control flow and binding. They are an improvement over anaphoric anaphoric macros because they let the developer communicate meaning through naming. As such their use is recommended over their anaphoric counterparts. (if-let (user (get-user user-id)) (show-dashboard user) (...
A common use of macros is to create templates for data structures which obey common rules but may contain different fields. By writing a macro, you can allow the detailed configuration of the data structure to be specified without needing to repeat boilerplate code, nor to use a less efficient struc...

Page 1 of 1