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-0
- The range is the last number till that loop ends.
- The statements are the things that are done inside the looping
Example: LISP Program to print cubes of first 20 numbers
Lisp
;define range upto 20
( dotimes (n 20 )
;display cube of each number
(print ( * n( * n n)))
)
|
Output:
0
1
8
27
64
125
216
343
512
729
1000
1331
1728
2197
2744
3375
4096
4913
5832
6859
Example 2: LISP program to display the sum of each number.
Lisp
;define range upto 20
( dotimes (n 20 )
;display sum of each number
(print ( + n n))
)
|
Output:
0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
Last Updated :
22 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...