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
Variable in Maths
A variable is like a placeholder or a box that can hold different values. In math, it's often represented by a letter, like x or y. The value of a variable can change depending on the situation. For example, if you have the equation y = 2x + 3, the value of y depends on the value of x. So, if you ch
5 min read
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
6 min read
Spring - Variable in SpEL
EvaluationContext interface is implemented by the StandardEvaluationContext class. To resolve properties or methods, it employs a reflection technique. The method setVariable on the StandardEvaluationContext can be used to set a variable. Using the notation #variableName, we may utilize this variabl
3 min read
Packages in LISP
Packages are a central mechanism in which a Common Lisp is used in managing different sets of names and avoiding name collisions that may occur if multiple files contain the same variables or functions with the same name. Understanding the package system to correctly split our code across multiple f
2 min read
ES6 | Variables
A variable is a named container in the memory, that stores the values. In simple words, we can say that a variable is a container for values in Javascript. The ES6 Variable names are called identifiers. The rules to keep in mind while naming an identifier. An identifier can contain alphabets and num
4 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
Set - $VARIABLE" in bash
In the world of bash scripting, you may come across the phrase "set - $VARIABLE". But what does it mean? At its most basic, "set - $VARIABLE" is used to split the value of a bash variable into separate words, using the Internal Field Separator (IFS) as the delimiter. For example, if VARIABLE has the
3 min read
Instance Variables in Ruby
There are four different types of variables in Ruby- Local variables, Instance variables, Class variables and Global variables. An instance variable in ruby has a name starting with @ symbol, and its content is restricted to whatever the object itself refers to. Two separate objects, even though the
3 min read