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,25 @@
|
||||
;;; 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)))
|
||||
@@ -0,0 +1,22 @@
|
||||
;;; SEQUENCES 2: REMOVE, REMOVE-IF, SUBSTITUTE (solution)
|
||||
|
||||
(defun drop-them (x xs)
|
||||
(remove x xs))
|
||||
|
||||
(defun drop-negatives (xs)
|
||||
(remove-if #'minusp xs))
|
||||
|
||||
(defun swap-them (new old xs)
|
||||
(substitute new old 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,25 @@
|
||||
;;; SEQUENCES 3: SORT, :KEY, REVERSE (solution)
|
||||
|
||||
(defun ascending (xs)
|
||||
(sort xs #'<))
|
||||
|
||||
(defun by-price (alist)
|
||||
(sort alist #'< :key #'cdr))
|
||||
|
||||
(defun back-words (xs)
|
||||
(mapcar #'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,27 @@
|
||||
;;; SEQUENCES 4: EVERY, SOME, SEARCH, MERGE (solution)
|
||||
|
||||
(defun all-fit (f xs)
|
||||
(every f xs))
|
||||
|
||||
(defun needle-at (needle hay)
|
||||
(search needle hay))
|
||||
|
||||
(defun weave (a b)
|
||||
(merge 'list 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