August 19, 2024
P03 - Find the K'th element of a list
The first element in the list is number 1.
Example:* (element-at '(a b c d e) 3)C
lisp
;;; Function that returns the element in the K-th position of ;;;
;;; a list. If the desired position is greater than ;;;
;;; last of the list, returns NIL. ;;
(defun element-at (org-list pos &optional (ini 1))
(if (eql ini pos)
(car org-list)
(element-at (cdr org-list) pos (+ ini 1)))
;;; Other solution
(defun element-at (list n)
(if (= n 1)
;; the first element is in position 1
(first list)
(element-at (rest list) (1-n))
)
)