LISP Tutorial
LISP Useful Resources
Selected Reading
- LISP - CLOS
- LISP - Error Handling
- LISP - Packages
- LISP - Structures
- LISP - File I/O
- LISP - Input & Output
- LISP - Hash Table
- LISP - Tree
- LISP - Set
- LISP - Vectors
- LISP - Symbols
- LISP - Lists
- LISP - Sequences
- LISP - Strings
- LISP - Arrays
- LISP - Characters
- LISP - Numbers
- LISP - Predicates
- LISP - Functions
- LISP - Loops
- LISP - Decisions
- LISP - Operators
- LISP - Constants
- LISP - Variables
- LISP - Macros
- LISP - Data Types
- LISP - Basic Syntax
- LISP - Program Structure
- LISP - Environment
- LISP - Overview
- LISP - Home
LISP Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
LISP - Macros
LISP - Macros
Macros allow you to extend the syntax of standard LISP.
Technically, a macro is a function that takes an s-expression as arguments and returns a LISP form, which is then evaluated.
Defining a Macro
In LISP, a named macro is defined using another macro named defmacro. Syntax for defining a macro is −
(defmacro macro-name (parameter-pst)) "Optional documentation string." body-form
The macro definition consists of the name of the macro, a parameter pst, an optional documentation string, and a body of Lisp expressions that defines the job to be performed by the macro.
Example
Let us write a simple macro named setTo10, which will take a number and set its value to 10.
Create new source code file named main.psp and type the following code in it.
(defmacro setTo10(num) (setq num 10)(print num)) (setq x 25) (print x) (setTo10 x)
When you cpck the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is −
25 10Advertisements