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]
.
Get list element by position
Asked Answered
In Common Lisp the operator that gets the n-th element of a list is called nth
(see the manual):
(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 2 '(a b c d)) ; returns (C D)
For an operator that works on vectors and proper lists, see elt
.
(let ((list (list 'a 'b 'c 'd)))
(prog1 list
(setf (elt list 1) 1)))
=> (A 1 C D)
© 2022 - 2024 — McMap. All rights reserved.
return lab[0]
– Louiselouisette(nth index list)
, with index starting from 0. – Tango