Emacs Lisp: Variable

By Xah Lee. Date: . Last updated: .

Global Variables

setq is used to set variables. Variables need not be declared, and is global.

;; assign 1 to x
(setq x 1) ; 1
;; return the value
;; multiple assignment
(setq a 3 b 2 c 7) ; 7
;; return the last value

Local Variables

To define local variables, use let. The form is:

(let (var1 var2 ) body)

where body is (one or more) lisp expressions. The body's last expression's value is returned.

(let (a b)
 (setq a 3)
 (setq b 4)
 (+ a b)
) ;  7

Another form of let is this:

(let ((var1 val1) (var2 val2) ) body)

(let ((a 3) (b 4))
 (+ a b)
) ;  7

This form lets you set values to variable without using many setq in the body. This form is convenient if you just have a few simple local vars with known values.

(info "(elisp) Variables")

Lisp Basics


Lisp Basics

Basics

Lisp Data Structure

Function

Lisp Symbol

Lisp Misc

Working with Elisp