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:
- Scalar types
- 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.
array | atom | bignum | bit |
bit-vector | character | [common] | compiled-function |
complex | cons | double-float | fixnum |
float | function | hash-table | integer |
keyword | list | long-float | nil |
null | number | package | pathname |
random-state | ratio | rational | readtable |
sequence | short-float | signed-byte | simple-array |
simple-bit-vector | simple-string | simple-vector | single-float |
standard-char | stream | string | [string-char] |
symbol | t | unsigned-byte | vector |
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.
Similar Reads
PL/ SQL Data Types
PL/SQL (Procedural Language/Structured Query Language) is a procedural extension language for SQL used specifically for the Oracle database to ease the management of data and the flow of operations. A core feature of PL/SQL is its diverse set of data types, designed to handle everything from simple
6 min read
R Data Types
R Data types are used to specify the kind of data that can be stored in a variable. For effective memory consumption and computation, the right data type must be selected. Each R data type has its own set of regulations and restrictions.Variables are not needed to be declare with a data type in R, d
7 min read
Data Types in Programming
In Programming, data type is an attribute associated with a piece of data that tells a computer system how to interpret its value. Understanding data types ensures that data is collected in the preferred format and that the value of each property is as expected. Data Types in Programming Table of Co
11 min read
MATLAB - Data Types
MATLAB is a platform which provides millions of Engineers and Scientists to analyze data using programming and numerical computing algorithm and also help in creating models. Data types are particular types of data items defined by the values they can store in them, generally, in programming languag
5 min read
Perl List and its Types
Introduction to Lists A list is a collection of scalar values. We can access the elements of a list using indexes. Index starts with 0 (0th index refers to the first element of the list). We use parenthesis and comma operators to construct a list. In Perl, scalar variables start with a $ symbol wher
4 min read
Sets in LISP
A set is an unordered collection of items. A set is just like other data structures. In C++ we have a similar data structure called a hash map. Common lisp does not provide a built-in set data type, but it provides a number of functions that allow set operations to be performed onto a list. Using th
7 min read
Tree in LISP
A tree is a non-linear hierarchical data structure that consists of nodes that are connected by edges. Tree stores data in a non-sequential manner so that operations like addition, deletion, updating, or searching could be performed in much less time than what it would take in linear data structures
5 min read
Hash Table in LISP
A hash table is a type of collection in Common LISP, that is used to map keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. Â Types of
3 min read
Atoms in LISP
Pre-requisites: Introduction to LISP Function Languages are those languages in which the basic building block is functions. In functional programming, the programmer is concerned only with functionality and not memory related variable storage and assignment sequence. There are some commonly used Fun
2 min read
Lists in LISP
Lists in common LISP is simply a single Linked list. In LISP, Lists are designed as a chain of records. While talking about record structures in LISP, the concept of Cons is vital. Cons in LISP is a record structure with 2 Â primary components. A cons function takes in 2 arguments and returns a new c
2 min read