May 2018
Beginner to intermediate
290 pages
6h 43m
English
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:
Read now
Unlock full access