Open In App

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:

Example 1:  




// 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 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:




//  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:




// 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:

 


Article Tags :