Open In App

Dolist Construct in LISP

Last Updated : 21 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

DoList in Common LISP is a looping statement used to iterate the elements in a list.

Syntax:

(dolist input_list)
 statements...
)

Here,

  • The input_list contains the list of elements that are iterated.
  • The statements are present in the loop.

Example 1: LISP Program to iterate the list of elements from 1 to 5.

Lisp




;create a dolist of 1 to 5 elements in  a list
(dolist (n '(1 2 3 4 5 ))
 
;iterate elements
   (format t "~% List of elements: ~d " n)
)


 
 

Output:

 

List of elements: 1  
List of elements: 2  
List of elements: 3  
List of elements: 4  
List of elements: 5 

 

Example 2: LISP Program to find the square of each element by iteration.

 

Lisp




;create a dolist of 1 to 5 elements in  a list
(dolist (n '(1 2 3 4 5 ))
 
  ;iterate elements to print each element
  (format t "~% Element's value: ~d " n)
   
  ;iterate elements to find square of each element
  (format t "~% Square of element: ~d " (* n n))
  (terpri)
)


 
 

Output:

 Element's value: 1 
 Square of element: 1 

 Element's value: 2 
 Square of element: 4 

 Element's value: 3 
 Square of element: 9 

 Element's value: 4 
 Square of element: 16 

 Element's value: 5 
 Square of element: 25 

 


Similar Reads

Dotimes Construct in LISP
In this article, we will discuss the dotimes loop in LISP. The dotimes is a looping statement used to iterate the elements. Unlike other looping constructs, it only loops for a specified number of iterations. Syntax: (dotimes (n range) statements --------------- -------------- ) where, n is the starting number-0The range is the last number till tha
1 min read
Loop For Construct in LISP
The loop for construct in common LISP is used to iterate over an iterable, similar to the for loop in other programming languages. It can be used for the following: This is used to set up variables for iteration.It can be used for conditionally terminate the iteration.It can be used for operating on the iterated elements. The loop For construct can
2 min read
Cond Construct in LISP
In this article, we will discuss cond construct in LISP. The cond is a decision-making statement used to make n number of test conditions. It will check all the conditions. Syntax: (cond (condition1 statements) (condition2 statements) (condition3 statements) ... (conditionn statements) ) Here, The conditions specify different conditions - if condit
2 min read
If Construct in LISP
In this article, we will discuss the if construct in LISP. The if is a decision-making statement used to check whether the condition is right or wrong. The if the condition is right, then it will go inside the if block and execute the statements under if block. Otherwise, the statements are not executed. Syntax: (if (condition) then (statement 1)..
2 min read
Do Construct in LISP
In this article, we will discuss the Do Construct in LISP. Do construct is used to perform an iteration in a structured format. Syntax: (do ((variable1 value1 new updated value 1) (variable2 value2 new updated value 2) ------------------------------- ------------------------------- (variable n value n new updated value n) ...) (test statements) ) H
2 min read
When Construct in LISP
In this article, we will discuss the when construct. The when is a decision-making statement used to specify the decisions. It is similar to conditional statements. Syntax: (when (condition) (statements) ) where, condition is a test statement used to teststatements are the actions that will depend on the condition Example 1: LISP Program to check t
2 min read
Loop Construct in LISP
In this article, we will discuss Loop Construct. This Construct is used to iterate the data until it finds the return statement. And then it will stop iterating and return the results. Syntax: (loop (statements) condition return ) where, loop is the keywordstatements are used to iterate the loopcondition is used to specify the condition so that the
2 min read
Case Construct in LISP
In this article, we will discuss the case construct in LISP. This is used to check multiple test conditions at a time, unlike cond, if and when it allows multiple conditions. Syntax: (case (key_value) ((key1) (statement 1 .............. statement n) ) ((key2) (statement 1 .............. statement n) ) ................ ((keyn) (statement 1 .........
2 min read
Constants in LISP
In LISP all constants are global variables. Values of constant never change throughout the execution of the program. Defining constant in LISP: New global constants are defined using the DEFCONSTANT construct Syntax: (defconstant name initial-value-form "documentation-string") Example: Let's create a global constant whose value will contain the rat
2 min read
LISP - Comparison Operators on Characters & Strings
The contents of a field are compared to the contents of another field or a constant using Comparison operators. In Simple words, comparator operators are used to compare whether the two or more different values are equal or not. Comparison Operators on Character: Characters are not supported by numerical comparison functions and operators such as
3 min read
Article Tags :