Arrays in LISP
LISP, is a list processing, is a programming language widely used in working with data manipulation. LISP allows us to produce single or multiple-dimension arrays using the make-array function. An array can store any LISP object as its elements.
The index number of the array starts from 0 to the n-th term.
Attributes:
- The setf attribute gives a call to the function.
- The aref attribute allows accessing the name of the array and index value.
- The my-array attribute is used in creating the cells of the array.
- The dotimes attribute allows looping. Looping start from zero to the nth number defined by the user.
- The tepri attribute is used to produce a new line,
- The initial-content attribute is a sequence of nested structures.
Example 1:
Lisp
/ / Making a file array / / lisp to print names (setq myarray (make - array '() :initial - contents '(((Ayush Agarwal) (Piyush Goel)) ((Keshav Kedia) (Gaurav Garg)) )) ) (write myarray) (terpri) |
Output:
Example 2:
Lisp
/ / Lisp code for array / / Making a file array.lisp / / to print number from 10 to 19 . (write ( setf my - array (make - array '( 10 )))) (terpri) ( setf (aref my - array 0 ) 10 ) ( setf (aref my - array 1 ) 11 ) ( setf (aref my - array 2 ) 12 ) ( setf (aref my - array 3 ) 13 ) ( setf (aref my - array 4 ) 14 ) ( setf (aref my - array 5 ) 15 ) ( setf (aref my - array 6 ) 16 ) ( setf (aref my - array 7 ) 17 ) ( setf (aref my - array 8 ) 18 ) ( setf (aref my - array 9 ) 19 ) (write my - array) |
Output:
Example 3:
Lisp
/ / Printing a table for 0 and 1 / / using LISP array (setq a (make - array '( 2 11 ))) ( dotimes (i 2 ) ( dotimes (j 11 ) ( setf (aref a i j) ( list i 'x j ' = ( * i j))) ) ) ( dotimes (i 2 ) ( dotimes (j 11 ) (print (aref a i j)) ) ) |
Output:
Example 4:
Lisp
/ / Making a file array. / / lisp to print alphabets from A to Z. (setq myarray (make - array '() :initial - contents '(((a b c) (d e f)) ((g h i) (j k l)) ((m n o) (p q r) (s t u) (v w x y z)) )) ) (write myarray) (terpri) |
Output: