August 19, 2024
P18 - Extract a slice from a list
Given two indices, I and K, the slice is the list containing the elements between the I'th and K'th element of the original list (both limits included). Start counting the elements with 1.
Example:* (slice '(a b c d e f g h i k) 3 7)(C D E F G)
lisp
;;; function that receives a list and two positions I and K and
;;; returns the part of the original list between positions
;;; I and K inclusive. Positions start from 1.
;;
;;; Extreme cases:
;;; if I <= 0, returns from 1 to K
;;; if K > list length, returns from I to the end
;;; if I > K, returns empty list
;;
;;; Note: uses function as auxiliary function to split the
;;; problem p17.
(defun slice (list i k)
(cond
((<= i 1) (split-up-to list k))
((null list) list)
(t (slice (cdr list) (1-i) (1-k))
)
)