Wave 4: add sequences/vectors/files chapters, 23 exercises, quiz3, finish ceremony, PCL table
This commit is contained in:
1 parent
77843402fa
commit
4675bd1106
76 files changed
+2505
-14
No files matched your search
@@ -0,0 +1,7 @@
|
||||
# 19_sequences
|
||||
|
||||
The generic sequence toolbox: FIND, REMOVE, SORT, SEARCH, MERGE.
|
||||
|
||||
Lists and vectors answer to the same functions, guided by :test
|
||||
and :key. One set of names counts, filters, and rearranges either,
|
||||
and EVERY and SOME ask questions of the whole.
|
||||
@@ -0,0 +1,37 @@
|
||||
;;; SEQUENCES 1: FIND, POSITION, COUNT
|
||||
;;;
|
||||
;;; A sequence is anything you can walk element by element: a list,
|
||||
;;; a vector, a string. One toolbox serves them all.
|
||||
;;; (find 2 '(1 2 3)) => 2 the element itself, or NIL
|
||||
;;; (position 2 #(1 2 3)) => 1 where it sits, or NIL
|
||||
;;; (count 'a '(a b a)) => 2 how many match
|
||||
;;; :test decides what counts as a match. The default, EQL, treats
|
||||
;;; 1 and 1.0 as different numbers; #'= does not.
|
||||
|
||||
;; TODO: The index where X sits in XS, or NIL.
|
||||
(defun index-of (x xs)
|
||||
(find x xs))
|
||||
|
||||
;; TODO: How many times X occurs in XS.
|
||||
(defun occurrences (x xs)
|
||||
(position x xs))
|
||||
|
||||
;; TODO: The first element of XS numerically equal to X, so that
|
||||
;;; 1.0 can find the integer 1.
|
||||
(defun find-same (x xs)
|
||||
(find x xs))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; 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)))
|
||||
@@ -0,0 +1,33 @@
|
||||
;;; SEQUENCES 2: REMOVE, REMOVE-IF, SUBSTITUTE
|
||||
;;;
|
||||
;;; All three return a fresh sequence and leave their input alone.
|
||||
;;; REMOVE drops the matches; REMOVE-IF drops everything its
|
||||
;;; predicate accepts; SUBSTITUTE swaps every match for a new
|
||||
;;; element, reading its arguments as NEW, OLD, then the sequence.
|
||||
;;; (remove 'b '(a b c b)) => (A C)
|
||||
;;; (remove-if #'minusp '(-1 0 2)) => (0 2)
|
||||
;;; (substitute 'x 1 '(1 2 1)) => (X 2 X)
|
||||
|
||||
;; TODO: XS with every occurrence of X dropped.
|
||||
(defun drop-them (x xs)
|
||||
(remove xs x))
|
||||
|
||||
;; TODO: XS with every negative number removed.
|
||||
(defun drop-negatives (xs)
|
||||
(remove-if #'plusp xs))
|
||||
|
||||
;; TODO: XS with every occurrence of OLD replaced by NEW.
|
||||
(defun swap-them (new old xs)
|
||||
(substitute old new xs))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal '(a c) (drop-them 'b '(a b c b)))
|
||||
(check-equal '(1 3) (drop-them 2 '(2 1 2 3 2)))
|
||||
(check-equal '(1 0 2) (drop-negatives '(-1 1 0 -2 2)))
|
||||
(check-equal '(3) (drop-negatives '(-5 -3 3)))
|
||||
(check-equal '(x 2 x) (swap-them 'x 1 '(1 2 1)))
|
||||
(check-equal '(a b a) (swap-them 'a 'z '(z b z)))
|
||||
@@ -0,0 +1,38 @@
|
||||
;;; SEQUENCES 3: SORT, :KEY, REVERSE
|
||||
;;;
|
||||
;;; SORT arranges a sequence by a comparison function:
|
||||
;;; (sort (list 3 1 2) #'<) => (1 2 3)
|
||||
;;; SORT is allowed to rearrange the very sequence you hand it, so
|
||||
;;; pass it a fresh one - the checks below all build with LIST.
|
||||
;;; :key picks out the part to compare:
|
||||
;;; (sort pairs #'< :key #'cdr) sorts by each pair's price
|
||||
;;; REVERSE returns a backwards copy, and its input stays as it
|
||||
;;; was: (reverse '(1 2 3)) => (3 2 1)
|
||||
|
||||
;; TODO: XS from smallest to largest.
|
||||
(defun ascending (xs)
|
||||
(sort xs #'>))
|
||||
|
||||
;; TODO: ALIST, a list of (item . price) pairs, sorted cheapest
|
||||
;;; first.
|
||||
(defun by-price (alist)
|
||||
(sort alist #'< :key #'car))
|
||||
|
||||
;; TODO: Each word of XS reversed, with the words still in order.
|
||||
(defun back-words (xs)
|
||||
(reverse xs))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal '(1 2 3) (ascending (list 3 1 2)))
|
||||
(check-equal '(-1 0 4) (ascending (list 4 0 -1)))
|
||||
(check-equal '(3 2 1) (reverse (list 1 2 3)))
|
||||
(check-equal '((tea . 1) (cola . 2))
|
||||
(by-price (list (cons 'cola 2) (cons 'tea 1))))
|
||||
(check-equal '((b . 1) (c . 2) (a . 7))
|
||||
(by-price (list (cons 'a 7) (cons 'b 1) (cons 'c 2))))
|
||||
(check-equal '("tac" "esaelp") (back-words '("cat" "please")))
|
||||
(check-equal '("bba" "aa") (back-words '("abb" "aa")))
|
||||
@@ -0,0 +1,44 @@
|
||||
;;; SEQUENCES 4: EVERY, SOME, SEARCH, MERGE
|
||||
;;;
|
||||
;;; EVERY and SOME ask a question of a whole sequence. EVERY wants
|
||||
;;; the predicate true of each element - the empty list qualifies.
|
||||
;;; SOME settles for one:
|
||||
;;; (every #'plusp '(1 2)) => T, (every #'plusp '()) => T
|
||||
;;; (some #'minusp '(1 -2)) => T
|
||||
;;; SEARCH hunts for a whole subsequence, not one element, and
|
||||
;;; returns where it starts, or NIL:
|
||||
;;; (search '(b c) '(a b c d)) => 1
|
||||
;;; MERGE interleaves two sequences that are already in order, by
|
||||
;;; the same kind of comparison SORT takes. Like SORT it may reuse
|
||||
;;; its arguments:
|
||||
;;; (merge 'list (list 1 3) (list 2 4) #'<) => (1 2 3 4)
|
||||
|
||||
;; TODO: T when every element of XS satisfies F, NIL otherwise.
|
||||
(defun all-fit (f xs)
|
||||
(some f xs))
|
||||
|
||||
;; TODO: The index where NEEDLE starts inside HAY, or NIL.
|
||||
(defun needle-at (needle hay)
|
||||
(find needle hay))
|
||||
|
||||
;; TODO: The elements of A and B together, in ascending order. A
|
||||
;;; and B arrive already sorted.
|
||||
(defun weave (a b)
|
||||
(append a b))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal t (all-fit #'plusp '(1 2 3)))
|
||||
(check-equal nil (all-fit #'plusp '(-1 2 3)))
|
||||
(check-equal t (all-fit #'plusp '()))
|
||||
(check (some #'minusp '(1 -2 3)))
|
||||
(check-equal 1 (needle-at '(b c) '(a b c d)))
|
||||
(check-equal 2 (needle-at '(2 3) '(1 5 2 3)))
|
||||
(check-equal 1 (needle-at "ana" "banana"))
|
||||
(check-equal nil (needle-at '(9) '(1 2 3)))
|
||||
(check-equal '(1 2 3 4) (weave (list 1 3) (list 2 4)))
|
||||
(check-equal '(0 1 4 9) (weave (list 4) (list 0 1 9)))
|
||||
(check-equal '(2 3) (weave (list 2 3) (list)))
|
||||
Reference in new issue
Block a user