Files
lisplings/solutions/19_sequences/sequences1.lisp
T

26 lines
789 B
Common Lisp

;;; SEQUENCES 1: FIND, POSITION, COUNT (solution)
(defun index-of (x xs)
(position x xs))
(defun occurrences (x xs)
(count x xs))
(defun find-same (x xs)
(find x xs :test #'=))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 1 (index-of 'b #(a b c)))
(check-equal 2 (index-of 3 '(1 2 3)))
(check-equal nil (index-of 9 '(1 2 3)))
(check-equal 3 (occurrences 'a #(a b a c a)))
(check-equal 2 (occurrences 'b '(a b a b a)))
(check-equal 0 (occurrences 5 '(1 2 3)))
(check-equal 1 (find-same 1.0 '(2 1 3)))
(check-equal 2 (find-same 2.0 '(1 2 3)))
(check-equal nil (find-same 9 '(1 2 3)))