Variables and Data Types in Lisp

May 21, 2024

Learn about variables and data types in Lisp in this post. We will cover how to declare variables, the different data types available in Lisp, and how to work with them in your programs.

Variables in Lisp

In Lisp, variables are declared using the defvar or setq forms. The defvar form is used to declare a global variable, while setq is used to assign a value to a variable.

(defvar my-variable 10)
(setq another-variable 20)

Data Types in Lisp

Lisp has several data types, including:

  • Numbers: Integers, floating-point numbers
  • Strings: Textual data enclosed in double quotes
  • Lists: Ordered collections of elements enclosed in parentheses
  • Symbols: Represent names or identifiers

Working with Data Types

Here are some examples of working with different data types in Lisp:

Numbers

(+ 5 10)
(* 3 4)

Strings

(concatenate 'string "Hello, " "World!")

Lists

(cons 1 '(2 3 4))
(car '(1 2 3))
(cdr '(1 2 3))

Symbols

(defun greet (name)
<br/> (format t "Hello, ~a!" name))
(greet 'Lisp)