new from koans, loops and coding

- loops can be "ON" a list, then iterate over structure, and can be BY 'cddr and such
- looping over hashtables seems painful
- during coding didn't think about getting debugger,
  couldn't figure out how to do it without BREAK, and in DO loop didn't
  see separate frames
- again remembered MAKUNBOUND for if I try to set values during debugger
This commit is contained in:
efim 2022-07-31 12:14:21 +00:00
parent 9c64cf128f
commit a9bdaaccee
3 changed files with 103 additions and 56 deletions

View File

@ -14,18 +14,18 @@
(define-test if
;; IF only evaluates and returns one branch of a conditional expression.
(assert-equal ____ (if t :true :false))
(assert-equal ____ (if nil :true :false))
(assert-equal :true (if t :true :false))
(assert-equal :false (if nil :true :false))
;; This also applies to side effects that migh or might not be evaluated.
(let ((result))
(if t
(setf result :true)
(setf result :false))
(assert-equal ____ result)
(assert-equal :true result)
(if nil
(setf result :true)
(setf result :false))
(assert-equal ____ result)))
(assert-equal :false result)))
(define-test when-unless
;; WHEN and UNLESS are like one-branched IF statements.
@ -40,14 +40,14 @@
(unless (> x 5)
(setf unless-result x)
(push x unless-numbers)))
(assert-equal ____ when-result)
(assert-equal ____ when-numbers)
(assert-equal ____ unless-result)
(assert-equal ____ unless-numbers)))
(assert-equal 10 when-result)
(assert-equal '(10 9 8 7 6) when-numbers)
(assert-equal 5 unless-result)
(assert-equal '(5 4 3 2 1) unless-numbers)))
(define-test and-short-circuit
;; AND only evaluates forms until one evaluates to NIL.
(assert-equal ____
(assert-equal 5
(let ((x 0))
(and
(setf x (+ 2 x))
@ -58,7 +58,7 @@
(define-test or-short-circuit
;; OR only evaluates forms until one evaluates to non-NIL.
(assert-equal ____
(assert-equal 2
(let ((x 0))
(or
(setf x (+ 2 x))

View File

@ -20,70 +20,71 @@
(let* ((result-1 (loop for letter in '(#\a #\b #\c #\d) collect letter))
(result-2 (loop for number in '(1 2 3 4 5) sum number))
(result-3 (loop for list in '((foo) (bar) (baz)) append list)))
(assert-equal ____ result-1)
(assert-equal ____ result-2)
(assert-equal ____ result-3)))
(assert-equal '(#\a #\b #\c #\d) result-1)
(assert-equal 15 result-2)
(assert-equal '(foo bar baz) result-3)))
(define-test loop-multiple-variables
;; With multiple FOR clauses, the loop ends when any of the provided lists are
;; exhausted.
(let* ((letters '(:a :b :c :d))
(result (loop for letter in letters
for i from 1 to 1000
for i from 1 to 1000 ; so this advances all by one step
collect (list i letter))))
(assert-equal ____ result)))
(assert-equal '((1 :a) (2 :b) (3 :c) (4 :d)) result)))
(define-test loop-in-versus-loop-on
;; Instead of iterating over each element of a list, we can iterate over each
;; cons cell of a list.
(let* ((letters '(:a :b :c))
(result-in (loop for thing in letters collect thing))
(result-on (loop for thing on letters collect thing)))
(assert-equal ____ result-in)
(assert-equal ____ result-on)))
(result-on (loop for thing on letters collect thing))) ; this is unusual, how's that used
(assert-equal '(:a :b :c) result-in)
(assert-equal '((:a :b :c) (:b :c) (:c)) result-on)))
(define-test loop-for-by
;; Numeric iteration can go faster or slower if we use the BY keyword.
(let* ((result (loop for i from 0 to 30 by 5 collect i)))
(assert-equal ____ result)))
(assert-equal '(0 5 10 15 20 25 30) result)))
(define-test loop-counting-backwards
;; We can count downwards instead of upwards by using DOWNTO instead of TO.
(let ((result (loop for i from 5 downto -5 collect i)))
(assert-equal ____ result)))
(assert-equal '(5 4 3 2 1 0 -1 -2 -3 -4 -5) result)))
(define-test loop-list-by
;; List iteration can go faster or slower if we use the BY keyword.
(let* ((letters '(:a :b :c :d :e :f))
(result (loop for letter in letters collect letter))
(result-cdr (loop for letter in letters by #'cdr collect letter))
(result-cdr (loop for letter in letters by #'cdr collect letter)) ; yup, figured that would be same as "by 1"
(result-cddr (loop for letter in letters by #'cddr collect letter))
(result-cdddr (loop for letter in letters by #'cdddr collect letter)))
(assert-equal ____ result)
(assert-equal ____ result-cdr)
(assert-equal ____ result-cddr)
(assert-equal ____ result-cdddr)))
(assert-equal '(:a :b :c :d :e :f) result)
(assert-equal '(:a :b :c :d :e :f) result-cdr)
(assert-equal '(:a :c :e) result-cddr)
(assert-equal '(:a :d) result-cdddr)))
(define-test loop-across
;; LOOP can iterate over a vector with the ACROSS keyword.
(let* ((vector (make-array '(5) :initial-contents '(0 1 2 3 4)))
(result (loop for number across vector collect number)))
(assert-equal ____ result)))
(assert-equal '(0 1 2 3 4) result))) ; why the hell separate keyword
; that seems unpleasant
(define-test loop-over-2d-array
(let ((array (make-array '(3 2) :initial-contents '((0 1) (2 3) (4 5)))))
;; LOOP can be combined with ROW-MAJOR-AREF to iterate over the contents of
;; a multidimensional array.
(let* ((result (loop for i from 0 below (array-total-size array)
collect (row-major-aref array i))))
(assert-equal ____ result))
;; It is always possible to resort to nested loops.
(let* ((result (loop with max-i = (array-dimension array 0)
for i from 0 below max-i
collect (loop with max-j = (array-dimension array 1)
for j from 0 below max-j
collect (expt (aref array i j) 2)))))
(assert-equal ____ result))))
(let ((array (make-array '(3 2) :initial-contents '((0 1) (2 3) (4 5)))))
;; LOOP can be combined with ROW-MAJOR-AREF to iterate over the contents of
;; a multidimensional array.
(let* ((result (loop for i from 0 below (array-total-size array)
collect (row-major-aref array i))))
(assert-equal '(0 1 2 3 4 5) result))
;; It is always possible to resort to nested loops.
(let* ((result (loop with max-i = (array-dimension array 0)
for i from 0 below max-i ; cool that keywords are easily compared "to" vs "below", but how the hekc do you remember that
collect (loop with max-j = (array-dimension array 1)
for j from 0 below max-j
collect (expt (aref array i j) 2)))))
(assert-equal '( (0 1) (4 9) (16 25)) result)))) ; didn't do nested at first try
(define-test loop-hash-table
(let ((book-heroes (make-hash-table :test 'equal)))
@ -93,10 +94,10 @@
(gethash "The Great Gatsby" book-heroes) "James Gatz")
;; LOOP can iterate over hash tables.
(let ((pairs-in-table (loop for key being the hash-keys of book-heroes
using (hash-value value)
using (hash-value value) ; have no idea what's that and how pliable this is
collect (list key value))))
(assert-equal ____ (length pairs-in-table))
(true-or-false? ____ (find '("The Hobbit" "Bilbo") pairs-in-table
(assert-equal 4 (length pairs-in-table))
(true-or-false? 0 (find '("The Hobbit" "Bilbo") pairs-in-table
:test #'equal)))))
(define-test loop-statistics
@ -110,31 +111,31 @@
finally (return (list collected counted summed
maximized minimized)))))
(destructuring-bind (collected counted summed maximized minimized) result
(assert-equal ____ collected)
(assert-equal ____ counted)
(assert-equal ____ summed)
(assert-equal ____ maximized)
(assert-equal ____ minimized))))
(assert-equal '(1 2 4 8 16 32) collected)
(assert-equal 6 counted)
(assert-equal 63 summed)
(assert-equal 32 maximized)
(assert-equal 1 minimized))))
(define-test loop-destructuring
;; LOOP can bind multiple variables on each iteration step.
(let* ((count 0)
(result (loop for (a b) in '((1 9) (2 8) (3 7) (4 6))
(result (loop for (a b) in '((1 9) (2 8) (3 7) (4 6)) ; now that's neat, just LOOP overall seems to complex
do (incf count)
collect (+ a b))))
(assert-equal ____ count)
(assert-equal ____ result)))
(assert-equal 4 count)
(assert-equal '(10 10 10 10) result)))
(define-test loop-conditional-execution
(let ((numbers '(1 1 2 3 5 8 13 21)))
;; LOOP can execute some actions conditionally.
(let ((result (loop for x in numbers
when (evenp x) sum x)))
(assert-equal ____ result))
(assert-equal 10 result))
(let ((result (loop for x in numbers
unless (evenp x) sum x)))
(assert-equal ____ result))
(assert-equal 44 result))
(flet ((greater-than-10-p (x) (> x 10)))
(let ((result (loop for x in numbers
when (greater-than-10-p x) sum x)))
(assert-equal ____ result)))))
(assert-equal 34 result)))))

View File

@ -49,11 +49,57 @@
;;;
;;; Your goal is to write the scoring function for Greed.
(defun score (&rest dice)
____)
(defvar *dice-evaluations* (let ((dice-evaluations-hash (make-hash-table :test #'equal :size 8)))
(setf (gethash '(1 1 1) dice-evaluations-hash) 1000
(gethash '(6 6 6) dice-evaluations-hash) 600
(gethash '(5 5 5) dice-evaluations-hash) 500
(gethash '(4 4 4) dice-evaluations-hash) 400
(gethash '(3 3 3) dice-evaluations-hash) 300
(gethash '(2 2 2) dice-evaluations-hash) 200
(gethash '(1) dice-evaluations-hash) 100
(gethash '(5) dice-evaluations-hash) 50)
dice-evaluations-hash))
;; iterate over dice. (on?)
;; (loop for rest-dice on '(1 2 5 1 1) collect rest-dice)
;; (subseq '(1 1 2 5) 0 2)
;; (subseq '(1 1 2 5) 0 1)
;; (gethash '(2) *dice-evaluations*)
(defun score-bad-1 (&rest dice)
(loop for rest-dice on (sort dice #'<)
sum (or
(gethash (subseq rest-dice 0 (min (length rest-dice) 3)) *dice-evaluations*)
(gethash (subseq rest-dice 0 (min (length rest-dice) 1)) *dice-evaluations*)
0)))
(defun take-n (list n)
(butlast list (max 0 (- (length list) n))))
(take-n '(1 1 3 2) 5)
;; (makunbound 'dice)
;; (setq test-dice '(1 1 1 2 5))
(defun score (&rest dice) (do ((dice (sort dice #'<))
(running-sum 0)
(maybe-3-score (gethash (take-n dice 3) *dice-evaluations*) (gethash (take-n dice 3) *dice-evaluations*))
(maybe-1-score (gethash (take-n dice 1) *dice-evaluations*) (gethash (take-n dice 1) *dice-evaluations*)))
((eq nil dice) running-sum)
;; (break "fuck")
(if maybe-3-score (setf dice (cdddr dice)
running-sum (+ running-sum maybe-3-score))
(setf dice (cdr dice)
running-sum (+ running-sum (or maybe-1-score 0))))))
(score 1 5 1) ; yay, i think it works
; but not as comfortable as I imagined, or as could be?
; not at all comfortable with debugger, some shit is shown, not iteration step
(define-test score-of-an-empty-list-is-zero
(assert-equal 0 (score)))
(assert-equal 0 (score)))
(define-test score-of-a-single-roll-of-5-is-50
(assert-equal 50 (score 5)))