Chapter 3

 

Data types.

 

A cons is a  record structure consisting of car and cdr to construct lists. A list is defined  as empty list or list which consists of cons whose cdr is a list. A dotted list is one whose last cons is not a cdr but an object.

(a.4) is a dotted list whose car is a and cdr is 4

 
Cons function add an element to beginning of list.
 
(cons 'a 'b) => (a . b) 
(cons 'a (cons 'b (cons 'c '()))) => (a b c) 
(cons 'a '(b c d)) => (a b c d)

 

One dimensional array are called vectors. Vectors and list constitutes the type sequences. A vector is  defined as;

 

#(A B C)

CG-USER(3): (setq a #(a b c))

#(A B C)

CG-USER(4): a

#(A B C)

CG-USER(5): (vectorp a)

T

CG-USER(6):

 

 

A string is a vector whose elements type is string-char.

The string function char is used to access the elements the list.

[4] CG-USER(28): (char "faiz ul haque zeya" 1)

#\a

[4] CG-USER(29):

 

String= is used to compare two strings

 

[4] CG-USER(29): (string= "faiz" "faiz")

T

[4] CG-USER(30): (string=  "faiz1" "faiz")

NIL

[4] CG-USER(31):

 

String-upcase and string-downcase is used to convert string to uppercase and lowercase lowercase.

 

[4] CG-USER(31): (String-upcase "faiz Ul haque zeya")

"FAIZ UL HAQUE ZEYA"

[4] CG-USER(32): (String-downcase "faiz ul haque zEYA")

"faiz ul haque zeya"

[4] CG-USER(33):

 

 

 

 

 

 

A list is created using the function list

(list  1 2 3 4)

 Returns a list 1234.

 

List-length is a function that returns the length of list.

 

CG-USER(8): (list-length (list 1 2 3 4))

4

CG-USER(9):

 

Function nth returns the nth element of list.

Nth  k list returns the kth element of the list

 

CG-USER(10): (nth 1 (list 1 2 3 4))

2

CG-USER(11): (nth 0 (list 1 2 3 ))

1

CG-USER(12)

 

Last returns the last cons of the list

 

CG-USER(12):  (last (list 1 2 3 4))

(4)

CG-USER(13):

 

 

Nconc takes the list and concatenates them

 

CG-USER(14): (nconc (list 1 2 3 4) (list 1 2 3 4))

(1 2 3 4 1 2 3 4)

CG-USER(15):

 

Array in Lisp can have many dimensions including  zero.

Make-array is used to create array in LISP.

 

CG-USER(15): (setq a (make-array 4))

#(NIL NIL NIL NIL)

CG-USER(16): a

#(NIL NIL NIL NIL)

CG-USER(17): (setq b (make-array '(2 3)))

#2A((NIL NIL NIL) (NIL NIL NIL))

CG-USER(18): b

#2A((NIL NIL NIL) (NIL NIL NIL))

CG-USER(19):

 

[4] CG-USER(24): (make-array '(3 4) :initial-contents '((1 2 3 4)(1 3 4 5) (1 3 2 3)))

#2A((1 2 3 4) (1 3 4 5) (1 3 2 3))

[4] CG-USER(25):

 

 Creates two dimensional array of three elements of each having  4 elements.

 

 

 

 

 

 

 

 

 

(setq a (make-array 5 :initial-contents

                                     '( 1 2 3 4 5)))

#(1 2 3 4 5)

[4] CG-USER(26): a

#(1 2 3 4 5)

[4] CG-USER(27): (aref a 3)

4

[4] CG-USER(28):

 

Aref returns the nth element of list.

(setq a (make-array 5 :initial-contents

                                     '( 1 2 3 4 5)))

#(1 2 3 4 5)

[4] CG-USER(26): a

#(1 2 3 4 5)

[4] CG-USER(27): (aref a 3)

4

[4] CG-USER(28):

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Exercises

  1. Define a two dimensional array with elements ((1 2 3 4 )( 12 3 4 5 ))
  2. Write a function  that return 3rd element of the list. (list 1 2 3 4 5)
  3. Compare whether two strings “faiz” and “Faiz” are equal.