A Universal Interface

Getting at the contents of a sequence is easy—and familiar. You just call first to get the lead-off element:

 ;; Returns "Emma".
 
 (first (seq '(​"Emma"​ ​"Oliver Twist"​ ​"Robinson Crusoe"​)))

and rest to get everything except the first element:

 ;; Returns the sequence ("Oliver Twist" "Robinson Crusoe")
 
 (rest (seq '(​"Emma"​ ​"Oliver Twist"​ ​"Robinson Crusoe"​)))

Alternatively, you use next to get at the all but the first sequence. The difference between next and rest is that while rest of an empty sequence is an empty sequence, next of an empty sequence is nil. While most of the time rest works fine, occasionally nil comes in handy.

You can also add a new element to the front of your sequence with cons:

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.