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
Last Updated :
21 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...