Elisp: Sequence Functions

By Xah Lee. Date: . Last updated: .

Here's a collection of functions that works on list, vector, or both.

In elisp, some such function works on vector only, some on list only, some on both.

[see Elisp: Sequence: List, Array]

Note, this page does not include functions for alist nor plist.

[see Elisp: Association List]

[see Elisp: Property List]

Basic List/Array Functions

(number-sequence n m step) → returns a list of a range of numbers, from n to m, in increment of step.

List to Vector

(vconcat sequence1 sequence2 …) → join any sequence types and return a vector.

Vector to List

(append sequence1 sequence2 …) → join any sequence types and return a list. Warning: if you want it to return a propert list, the last element must be a list, or nil.

To String

(format "%s" sequence)

Get 1 Element

Sublist

Prepend, Append, Join

(cons x list) → Return a new list, with x added to front. (prepend)

(append sequence1 sequence2 …) → join any sequence types and return a list. Warning: if you want it to return a propert list, the last element must be a list, or nil.

Modify List Variable

Sequence Functions

These functions work on both list and vector (and string). (Those starting with “seq-” are new in emacs 25.1, released in 2016-09)

Some of these provide new functionalities, some gives a unified interface to old functions.

  1. sequencep
  2. elt
  3. copy-sequence
  4. reverse
  5. nreverse
  6. sort
  7. seq-elt
  8. seq-length
  9. seqp
  10. seq-drop
  11. seq-take
  12. seq-take-while
  13. seq-drop-while
  14. seq-do
  15. seq-map
  16. seq-mapn
  17. seq-filter
  18. seq-remove
  19. seq-reduce
  20. seq-some
  21. seq-find
  22. seq-every-p
  23. seq-empty-p
  24. seq-count
  25. seq-sort
  26. seq-contains
  27. seq-position
  28. seq-uniq
  29. seq-subseq
  30. seq-concatenate
  31. seq-mapcat
  32. seq-partition
  33. seq-intersection
  34. seq-difference
  35. seq-group-by
  36. seq-into
  37. seq-min
  38. seq-max
  39. seq-doseq
  40. seq-let

Check Existence

(member x list) → check if x is in list. If so, return a list starting with the first occurrence of object. Else return nil. Comparison done using equal.

(memq x list) → same as member, but comparison done using eq.

(memql x list) → same as member, but comparison done using eql.

(member "4" '("3" "4" "5")) ;; ("4" "5")

Note: (eq "s" "s") is false, and (eq 1.2 1.2) also false.

(The difference of eq and eql is that the latter will return t for same floating point numbers.)

(member-ignore-case x list) → same as member, except that x should be a string, and comparison ignores letter-case.

(member-ignore-case "A" '("b" "a")) ; ("a")

Delete All X in Sequence

(delete x sequence) → remove all x in sequence. The original sequence is destroyed. Returns a new sequence. The comparison is done with equal.

(setq xx '(3 4 5))

;; always set result to the same var. because original is usually destroyed
(setq xx (delete 4 xx)) ; (3 5)

;; on vector
(setq xx [3 4 5])
(setq xx (delete 4 xx)) ; [3 5]

(remove x sequence) → same as delete, except sequence is not destroyed.

(setq xx '(3 4 5))
(remove 4 xx) ;; (3 5)
xx ; (3 4 5)

(delq x sequence) → Same as delete, except the comparison is done with eq.

(setq xx '(3 4 5))

;; always set result to the same var
(setq xx (delq 4 xx)) ; (3 5)

(remq x list) → same as delq, except it does not destroy the original list.

(setq xx '(3 4 5))
(remq 4 xx) ; (3 5)
xx ; (3 4 5)

Delete Duplicates

(delete-dups list) → This function destructively removes all duplicates from list, return a new list. The first one is kept among duplicates.

(setq xx '(3 4 5 3 2))
(setq xx (delete-dups xx)) ; (3 4 5 2)

Misc

Reference

(info "(elisp) Sequence Functions")

See also: (info "(elisp) Sets And Lists")

Elisp Data Structure

  1. Sequence: List, Array
  2. Vector
  3. List
  4. Sequence Functions
  5. Map List/Vector
  6. Exit Loop/Function
  7. Association List
  8. Property List
  9. Symbol Property List
  10. Hash Table
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.