Emacs Lisp: List
Emacs Lisp's list datatype is an ordered sequence of things. It is known as linked list data structure in computer science. It is an efficient datatype for stack data structure. (like a stack of books, allowing adding or removing one item at a time from the top in an efficient way.)
Lisp list are made up of lower structure called cons
, as nested Cons Pair.
Any item can be of any type.
Algorithm properties of lisp list:
- Fast and constant time to get value of first element. (by
car
) - Fast and constant time to get rest of list (i.e. without first item). (by
cdr
) - Fast and constant time to add one item to the front (beginning of list). (known as
push
) - Fast and constant time to remove the first element. (known as
pop
) - Any operation for nth element, its time is proportional to size of n.
Create List
Empty List, nil
In elisp, empty list is equivalent to nil. The following are all equivalent.
'()
(list)
- nil
(eq '() (list ) ) ; t (eq '() nil); t (eq (list ) nil ) ; t
Length
length
-
(length SEQUENCE)
return count of elements.
(equal (length '("a" "b" "c") ) 3)
Get Element from List
Add to List
cons
-
(cons new list)
Return a new list, with new added to front. (prepend)
(equal (cons "a" '("c" "d")) '("a" "c" "d")) (equal (cons '("a" "b") '("c" "d")) '(("a" "b") "c" "d"))
see also
• add-to-list
• add-to-ordered-list
• push
at
Emacs Lisp: Modify List
Modify List Variable
Append Join, Convert List
Reference
Lisp Data Structure
List
- Cons Pair
- Quote and Dot Notation
- Proper List
- List
- Create List
- List, Get Elements
- Modify List
- Check Element Exist in List
- Remove Elements in List
- Backquote Reader Macro