Reading — step 1 of 5
Learn
~2 min readPackages, Sequences, Pathnames
Common Lisp has a unified sequence API that works on lists, vectors, AND strings. Learn it once, use everywhere.
The fundamental sequence operators
(elt seq i) ; element at index i (works for list/vector/string)
(length seq) ; size
(subseq seq start &optional end)
Search and find
(find 3 '(1 2 3 4)) ; 3 — first match
(find 'a #(b c a d)) ; a
(position #\o "hello") ; 4
(count 1 '(1 2 1 3 1)) ; 3
(every #'evenp '(2 4 6)) ; t — all match
(some #'oddp '(1 2 3)) ; t — any match
(notany #'minusp '(1 2 3)) ; t — none match
-if variants take a predicate:
(find-if #'evenp '(1 2 3 4)) ; 2
(position-if #'(lambda (x) (> x 5)) '(1 3 7 9)) ; 2
(count-if #'oddp '(1 2 3 4 5)) ; 3
(remove-if #'evenp '(1 2 3 4 5)) ; (1 3 5)
(remove-if-not #'evenp '(1 2 3 4 5)) ; (2 4)
Map, reduce, filter
(mapcar #'1+ '(1 2 3)) ; (2 3 4) — list result
(map 'vector #'1+ #(1 2 3)) ; #(2 3 4) — specify result type
(map 'list #'+ '(1 2 3) '(10 20 30)) ; (11 22 33) — multi-arg
(reduce #'+ '(1 2 3 4 5)) ; 15
(reduce #'cons '(1 2 3 4) :from-end t) ; build a list
(reduce #'+ '(1 2 3) :initial-value 100) ; 106
Sort
(sort '(3 1 4 1 5 9 2 6) #'<) ; sorted ascending
(sort '("banana" "apple" "cherry") #'string<)
WARNING: sort is destructive — it may modify the original. Use (sort (copy-list lst) #'<) to preserve.
Stable sort: (stable-sort lst #'<) preserves relative order of equal elements.
Concat and split
(concatenate 'list '(1 2) '(3 4)) ; (1 2 3 4)
(concatenate 'vector '(1 2) #(3 4)) ; #(1 2 3 4)
(concatenate 'string "hi" " " "there") ; "hi there"
Reverse
(reverse '(1 2 3 4)) ; (4 3 2 1)
(reverse #(1 2 3 4)) ; #(4 3 2 1)
(reverse "hello") ; "olleh"
Why this matters
Most sequence operations are polymorphic. Code written for lists usually works for vectors and strings unchanged — except where the data structure matters (cons cells vs O(1) random access).
Know these names — they're the Lisp standard library's vocabulary.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…