Similar to other languages, in LISP variables are named places that store a particular value, but they are not declared the way you declare variables in C++ or Java i.e you don't need to mention data-type of variables when declaring them as LISP is dynamically typed.
LISP supports two types of variables:
- Local variables
- Global variables
Local Variables:
They are also called Lexical variables, these variables are defined inside a particular function and only the code within the binding form can refer to them as they can not be accessed outside of that function. Parameters of the functions are also local variables.
Defining a local variable:
New local variables can be created using the special operator LET. These variables then can be used within the body of the LET.
The syntax for creating a variable with LET is:
(let (variable)
body-of-expressions)
A let expression has two parts
- The first part consists of instructions for creating a variable and assigning values to them
- The second part consists of a list of s-expressions
Now let's create local variables using the above-mentioned syntax
Lisp
(let ((x 10) (y 20) z)
(format t "Local Variables :~%")
(format t "x = ~a, y = ~a & z=~a~%" x y z))
Here we have assigned x and y with initial values and for z no value is provided, hence NIL will be set as z's default value.
Output :
Local Variables :
x = 10, y = 20 & z=NIL
Global Variables:
These are the variables that have a global scope i.e. you can call access them from anywhere in the program. In LISP global variables are also called Dynamic variables.
Defining a global variable:
New global variables can be defined using DEFVAR and DEFPARAMETER construct. The difference between both of them is that DEFPARAMETER always assigns initial value and DEFVAR form can be used to create a global variable without giving it a value.
The syntax for creating global variables is:
(defvar *name* initial-value
"Documentation string")
(defparameter *name* initial-value
"Documentation string")
Example:
Let's create a global variable that holds the rate of the pencil as *pencil-rate*, and with help of one function calculate the total bill by multiplying the total number of pencils with our global variable *pencil-rate*.
Lisp
(defparameter *pencil-rate* 10
"Pencil rate is set to 10")
(defun calc-bill (n)
"Calculates the total bill by multiplying the number of pencils with its global rate"
(* *pencil-rate* n))
(write (calc-bill 20))
Output:
200
It is a proper naming convention to define global variables to start and end with asterisks because as global variables can be accessed anywhere in the program, there can be local variables with the same name, hence for avoiding this possibility by accident this naming convention is used. Example. *global*
Similar Reads
Special variables in Perl Special Variables in Perl are those which are already defined to carry out a specific function when required. The differentiating factor between a special Variable in Perl and a random character is the use of Punctuation mark after the variable, these could be @, $ or %, etc, for example, $_. Perl V
7 min read
Predicates in LISP In this article, we will discuss predicates. Predicates are similar to functions that will be used to test their arguments for conditions. They will return NIL if the conditions are not met, if the conditions are met, they will return T. Types of predicates: Below is a list of major Predicates with
5 min read
Scope of Variable in R In R, variables are the containers for storing data values. They are reference, or pointers, to an object in memory which means that whenever a variable is assigned to an instance, it gets mapped to that instance. A variable in R can store a vector, a group of vectors or a combination of many R obje
5 min read
Perl | Variables Variables in Perl are used to store and manipulate data throughout the program. When a variable is created it occupies memory space. The data type of a variable helps the interpreter to allocate memory and decide what to be stored in the reserved memory. Therefore, variables can store integers, deci
4 min read
Macros in LISP Macro is similar to a function in popular languages like C++, java, etc. that takes arguments and returns a LISP form to be evaluated. It is useful when the same code has to be executed with a few variable changes.For example, rather than writing the same code for squaring a number, we can define a
2 min read