Reputation: 1399
I want to give a number and return the element of this position. List lab = (R K K K K) and I want to know if something like this (position 1 lab) exists on lisp. Like in C return lab[1].
(R K K K K)
(position 1 lab)
return lab[1]
Upvotes: 10
Views: 17402
Reputation: 38967
For an operator that works on vectors and proper lists, see elt.
elt
(let ((list (list 'a 'b 'c 'd))) (prog1 list (setf (elt list 1) 1))) => (A 1 C D)
Upvotes: 5
Reputation: 27454
In Common Lisp the operator that gets the n-th element of a list is called nth (see the manual):
nth
(nth 2 '(a b c d)) ; returns C
A related operator is nthcdr that returns the rest of the list starting from the n-th element:
nthcdr
(nthcdr 2 '(a b c d)) ; returns (C D)
Upvotes: 24