Skip to content
Related Articles

Related Articles

Data Types in LISP

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 31 Jan, 2022

A data type is basically a collection of values that have the same type. We can also say that in lisp, every object belongs to a data type. An object in lisp is a data that is used and manipulated by the lisp code.

LISP provides various data types, and commonly used data types are array, number, string, float, stream, vector, etc. We can categorize these data types into two categories:

  1. Scalar types
  2. Data structures

Scalar types are the data types that are used to store single values. Scalar types are number, float, stream, etc.

Data structures are the data types that are used to store multiple values. Data structures are array, vector, etc.

In lisp, data is a set of lisp objects. A lisp object can belong to more than one data type. And if we want to find whether an object belongs to a data type, we can use the typeep function. It will check if an object belongs to a specific data type.

And to find out the data type of a given object, we can use the typeof function. The typeof function returns the data type of the object.

In lisp, a set of objects is demonstrated by a symbol t and no object or empty data type is represented by nil.

Type specifiers in LISP.

Type specifiers are the lisp objects like names or symbols that are used to identify the data type of object. In LISP, variables are not type specifiers because variables are not typed. Here are the system-defined types that we can use in the list. 

arrayatombignumbit
bit-vectorcharacter[common]compiled-function
complexconsdouble-floatfixnum
floatfunctionhash-tableinteger
keywordlistlong-floatnill
nullnumberpackagepathname
random-stateratiorationalreadtable
sequenceshort-floatsigned-bytesimple-array
simple-bit-vectorsimple-stringsimple-vectorsingle-float
standard-charstreamstring[string-char]
symboltunsigned-bytevector

We can also create our own custom user-defined data types. To do so, we have to use defstruct function. The symbols defined using the defstruct become valid symbols.

Let’s learn about some commonly used data types in LISP. Below are the data types and their corresponding type specifiers.

1. Numbers: Numbers are used for storing the integer and floating-point values. Numbers are represented by the symbol number. Lisp has two types of numbers: integer and floating-point. A floating-point number is a number with a decimal point. An integer is a whole number without a decimal point. Lisp also provides Cartesian complex numbers.

Example:

Lisp




;; Numbers in LISP
  
(setq a 1) ;; a is a number
  
(setq b 2.0) ;; b is a floating point number
  
(setq c 4.0e2) ;; c is a floating point number
  
(setq d (complex 1 2)) ;; d is a complex number
  
  
  
(print "a is " a) ;; a is 1
  
(print "b is " b) ;; b is 2.0
  
(print "c is " c) ;; c is 400.0
  
(print "d is " d) ;; d is (1.0+2.0i)

Output:

a is 1
b is 2.0
c is 400.0
d is (1.0+2.0i)

Here, the complex is the type specifier for complex numbers.

2. Characters: Characters are used to store single character values. Characters can be used to represent a single character. And a group of one or more characters can be represented by a string. A string is a sequence of characters, it is a one-dimensional array of characters.

Example:

Lisp




;; characters in LISP
  
(setq a ?c) ;; a is a character
(setq b "GeeksforGeeks") ;; b is a string
  
(print "a is " a) ;; a is c
(print "b is " b) ;; b is GeeksforGeeks

Output:

a is c
b is GeeksforGeeks

3. Arrays: Arrays are used to store multiple values. Arrays are a collection of objects in lisp. Arrays can be indexed by integers. That means an array can be accessed by using the index of the array. But the array must have a non-negative number of dimensions. Vectors are also called one-dimensional arrays. A can store any kind of data type. Strings are called one-dimensional arrays of characters. Bit vectors are also called one-dimensional arrays of bits. Bit vectors only store 0 or 1.

Example:

Lisp




;; Arrays in LISP
  
(setq a (array 1 2 3 4 5)) ;; a is an array
(setq v (vector 1 2 3 4 5)) ;; v is a vector
(setq b (bitvector 0 1 0 1 1)) ;; b is a bit vector
(setq s (string "Geeks")) ;; s is a string
  
(print "a is " a) ;; a is [1 2 3 4 5]
(print "v is " v) ;; v is [1 2 3 4 5]
(print "b is " b) ;; b is [0 1 0 1 1]
(print "s is " s) ;; s is Geeks

Output:

a is [1 2 3 4 5]
v is [1 2 3 4 5]
b is [0 1 0 1 1]
s is Geeks

Here, the word vector is a type specifier for vectors. The word array is a type specifier for arrays. The word bit vector is the type specifier for bit vectors. The word string is a type specifier for strings. 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!