Initial segment (first n elements) of a list
For any natural number
The following is a theorem:
(equal (length (take n l)) (nfix n))
If
(implies (and (integerp n) (true-listp l) (<= (length l) n)) (equal (take n l) (append l (make-list (- n (length l))))))
For related functions, see nthcdr and see butlast.
The guard for
Function:
(defun first-n-ac (i l ac) (declare (type (integer 0 *) i) (xargs :guard (and (true-listp l) (true-listp ac)))) (cond ((zp i) (revappend ac nil)) (t (first-n-ac (1- i) (cdr l) (cons (car l) ac)))))
Function:
(defun take (n l) (declare (xargs :guard (and (integerp n) (not (< n 0)) (true-listp l)))) (mbe :logic (if (zp n) nil (cons (car l) (take (1- n) (cdr l)))) :exec (first-n-ac n l nil)))