LISP Tutorial on LISP Data Types

in lisp, variables are not typed, but data objects are.

lisp data types can be categorized as.

  • scalar types − for example, number types, characters, symbols etc.

  • data structures − for example, lists, vectors, bit-vectors, and strings.

any variable can take any lisp object as its value, unless you have declared it explicitly.

although, it is not necessary to specify a data type for a lisp variable, however, it helps in certain loop expansions, in method declarations and some other situations that we will discuss in later chapters.

the data types are arranged into a hierarchy. a data type is a set of lisp objects and many objects may belong to one such set.

the typep predicate is used for finding whether an object belongs to a specific type.

the type-of function returns the data type of a given object.

type specifiers in lisp

type specifiers are system-defined symbols for data types.

array fixnum package simple-string
atom float pathname simple-vector
bignum function random-state single-float
bit hash-table ratio standard-char
bit-vector integer rational stream
character keyword readtable string
[common] list sequence [string-char]
compiled-function long-float short-float symbol
complex nill signed-byte t
cons null simple-array unsigned-byte
double-float number simple-bit-vector vector

apart from these system-defined types, you can create your own data types. when a structure type is defined using defstruct function, the name of the structure type becomes a valid type symbol.

example 1

create new source code file named main.lisp and type the following code in it.

(setq x 10)
(setq y 34.567)
(setq ch nil)
(setq n 123.78)
(setq bg 11.0e+4)
(setq r 124/2)

(print x)
(print y)
(print n)
(print ch)
(print bg)
(print r)

when you click the execute button, or type ctrl+e, lisp executes it immediately and the result returned is −

10 
34.567 
123.78 
nil 
110000.0 
62

example 2

next let's check the types of the variables used in the previous example. create new source code file named main. lisp and type the following code in it.

(defvar x 10)
(defvar y 34.567)
(defvar ch nil)
(defvar n 123.78)
(defvar bg 11.0e+4)
(defvar r 124/2)

(print (type-of x))
(print (type-of y))
(print (type-of n))
(print (type-of ch))
(print (type-of bg))
(print (type-of r))

when you click the execute button, or type ctrl+e, lisp executes it immediately and the result returned is −

(integer 0 281474976710655) 
single-float 
single-float 
null 
single-float 
(integer 0 281474976710655)