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:
parent
9c64cf128f
commit
a9bdaaccee
|
@ -14,18 +14,18 @@
|
||||||
|
|
||||||
(define-test if
|
(define-test if
|
||||||
;; IF only evaluates and returns one branch of a conditional expression.
|
;; IF only evaluates and returns one branch of a conditional expression.
|
||||||
(assert-equal ____ (if t :true :false))
|
(assert-equal :true (if t :true :false))
|
||||||
(assert-equal ____ (if nil :true :false))
|
(assert-equal :false (if nil :true :false))
|
||||||
;; This also applies to side effects that migh or might not be evaluated.
|
;; This also applies to side effects that migh or might not be evaluated.
|
||||||
(let ((result))
|
(let ((result))
|
||||||
(if t
|
(if t
|
||||||
(setf result :true)
|
(setf result :true)
|
||||||
(setf result :false))
|
(setf result :false))
|
||||||
(assert-equal ____ result)
|
(assert-equal :true result)
|
||||||
(if nil
|
(if nil
|
||||||
(setf result :true)
|
(setf result :true)
|
||||||
(setf result :false))
|
(setf result :false))
|
||||||
(assert-equal ____ result)))
|
(assert-equal :false result)))
|
||||||
|
|
||||||
(define-test when-unless
|
(define-test when-unless
|
||||||
;; WHEN and UNLESS are like one-branched IF statements.
|
;; WHEN and UNLESS are like one-branched IF statements.
|
||||||
|
@ -40,14 +40,14 @@
|
||||||
(unless (> x 5)
|
(unless (> x 5)
|
||||||
(setf unless-result x)
|
(setf unless-result x)
|
||||||
(push x unless-numbers)))
|
(push x unless-numbers)))
|
||||||
(assert-equal ____ when-result)
|
(assert-equal 10 when-result)
|
||||||
(assert-equal ____ when-numbers)
|
(assert-equal '(10 9 8 7 6) when-numbers)
|
||||||
(assert-equal ____ unless-result)
|
(assert-equal 5 unless-result)
|
||||||
(assert-equal ____ unless-numbers)))
|
(assert-equal '(5 4 3 2 1) unless-numbers)))
|
||||||
|
|
||||||
(define-test and-short-circuit
|
(define-test and-short-circuit
|
||||||
;; AND only evaluates forms until one evaluates to NIL.
|
;; AND only evaluates forms until one evaluates to NIL.
|
||||||
(assert-equal ____
|
(assert-equal 5
|
||||||
(let ((x 0))
|
(let ((x 0))
|
||||||
(and
|
(and
|
||||||
(setf x (+ 2 x))
|
(setf x (+ 2 x))
|
||||||
|
@ -58,7 +58,7 @@
|
||||||
|
|
||||||
(define-test or-short-circuit
|
(define-test or-short-circuit
|
||||||
;; OR only evaluates forms until one evaluates to non-NIL.
|
;; OR only evaluates forms until one evaluates to non-NIL.
|
||||||
(assert-equal ____
|
(assert-equal 2
|
||||||
(let ((x 0))
|
(let ((x 0))
|
||||||
(or
|
(or
|
||||||
(setf x (+ 2 x))
|
(setf x (+ 2 x))
|
||||||
|
|
|
@ -20,55 +20,56 @@
|
||||||
(let* ((result-1 (loop for letter in '(#\a #\b #\c #\d) collect letter))
|
(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-2 (loop for number in '(1 2 3 4 5) sum number))
|
||||||
(result-3 (loop for list in '((foo) (bar) (baz)) append list)))
|
(result-3 (loop for list in '((foo) (bar) (baz)) append list)))
|
||||||
(assert-equal ____ result-1)
|
(assert-equal '(#\a #\b #\c #\d) result-1)
|
||||||
(assert-equal ____ result-2)
|
(assert-equal 15 result-2)
|
||||||
(assert-equal ____ result-3)))
|
(assert-equal '(foo bar baz) result-3)))
|
||||||
|
|
||||||
(define-test loop-multiple-variables
|
(define-test loop-multiple-variables
|
||||||
;; With multiple FOR clauses, the loop ends when any of the provided lists are
|
;; With multiple FOR clauses, the loop ends when any of the provided lists are
|
||||||
;; exhausted.
|
;; exhausted.
|
||||||
(let* ((letters '(:a :b :c :d))
|
(let* ((letters '(:a :b :c :d))
|
||||||
(result (loop for letter in letters
|
(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))))
|
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
|
(define-test loop-in-versus-loop-on
|
||||||
;; Instead of iterating over each element of a list, we can iterate over each
|
;; Instead of iterating over each element of a list, we can iterate over each
|
||||||
;; cons cell of a list.
|
;; cons cell of a list.
|
||||||
(let* ((letters '(:a :b :c))
|
(let* ((letters '(:a :b :c))
|
||||||
(result-in (loop for thing in letters collect thing))
|
(result-in (loop for thing in letters collect thing))
|
||||||
(result-on (loop for thing on letters collect thing)))
|
(result-on (loop for thing on letters collect thing))) ; this is unusual, how's that used
|
||||||
(assert-equal ____ result-in)
|
(assert-equal '(:a :b :c) result-in)
|
||||||
(assert-equal ____ result-on)))
|
(assert-equal '((:a :b :c) (:b :c) (:c)) result-on)))
|
||||||
|
|
||||||
(define-test loop-for-by
|
(define-test loop-for-by
|
||||||
;; Numeric iteration can go faster or slower if we use the BY keyword.
|
;; 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)))
|
(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
|
(define-test loop-counting-backwards
|
||||||
;; We can count downwards instead of upwards by using DOWNTO instead of TO.
|
;; We can count downwards instead of upwards by using DOWNTO instead of TO.
|
||||||
(let ((result (loop for i from 5 downto -5 collect i)))
|
(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
|
(define-test loop-list-by
|
||||||
;; List iteration can go faster or slower if we use the BY keyword.
|
;; List iteration can go faster or slower if we use the BY keyword.
|
||||||
(let* ((letters '(:a :b :c :d :e :f))
|
(let* ((letters '(:a :b :c :d :e :f))
|
||||||
(result (loop for letter in letters collect letter))
|
(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-cddr (loop for letter in letters by #'cddr collect letter))
|
||||||
(result-cdddr (loop for letter in letters by #'cdddr collect letter)))
|
(result-cdddr (loop for letter in letters by #'cdddr collect letter)))
|
||||||
(assert-equal ____ result)
|
(assert-equal '(:a :b :c :d :e :f) result)
|
||||||
(assert-equal ____ result-cdr)
|
(assert-equal '(:a :b :c :d :e :f) result-cdr)
|
||||||
(assert-equal ____ result-cddr)
|
(assert-equal '(:a :c :e) result-cddr)
|
||||||
(assert-equal ____ result-cdddr)))
|
(assert-equal '(:a :d) result-cdddr)))
|
||||||
|
|
||||||
(define-test loop-across
|
(define-test loop-across
|
||||||
;; LOOP can iterate over a vector with the ACROSS keyword.
|
;; LOOP can iterate over a vector with the ACROSS keyword.
|
||||||
(let* ((vector (make-array '(5) :initial-contents '(0 1 2 3 4)))
|
(let* ((vector (make-array '(5) :initial-contents '(0 1 2 3 4)))
|
||||||
(result (loop for number across vector collect number)))
|
(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
|
(define-test loop-over-2d-array
|
||||||
(let ((array (make-array '(3 2) :initial-contents '((0 1) (2 3) (4 5)))))
|
(let ((array (make-array '(3 2) :initial-contents '((0 1) (2 3) (4 5)))))
|
||||||
|
@ -76,14 +77,14 @@
|
||||||
;; a multidimensional array.
|
;; a multidimensional array.
|
||||||
(let* ((result (loop for i from 0 below (array-total-size array)
|
(let* ((result (loop for i from 0 below (array-total-size array)
|
||||||
collect (row-major-aref array i))))
|
collect (row-major-aref array i))))
|
||||||
(assert-equal ____ result))
|
(assert-equal '(0 1 2 3 4 5) result))
|
||||||
;; It is always possible to resort to nested loops.
|
;; It is always possible to resort to nested loops.
|
||||||
(let* ((result (loop with max-i = (array-dimension array 0)
|
(let* ((result (loop with max-i = (array-dimension array 0)
|
||||||
for i from 0 below max-i
|
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)
|
collect (loop with max-j = (array-dimension array 1)
|
||||||
for j from 0 below max-j
|
for j from 0 below max-j
|
||||||
collect (expt (aref array i j) 2)))))
|
collect (expt (aref array i j) 2)))))
|
||||||
(assert-equal ____ result))))
|
(assert-equal '( (0 1) (4 9) (16 25)) result)))) ; didn't do nested at first try
|
||||||
|
|
||||||
(define-test loop-hash-table
|
(define-test loop-hash-table
|
||||||
(let ((book-heroes (make-hash-table :test 'equal)))
|
(let ((book-heroes (make-hash-table :test 'equal)))
|
||||||
|
@ -93,10 +94,10 @@
|
||||||
(gethash "The Great Gatsby" book-heroes) "James Gatz")
|
(gethash "The Great Gatsby" book-heroes) "James Gatz")
|
||||||
;; LOOP can iterate over hash tables.
|
;; LOOP can iterate over hash tables.
|
||||||
(let ((pairs-in-table (loop for key being the hash-keys of book-heroes
|
(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))))
|
collect (list key value))))
|
||||||
(assert-equal ____ (length pairs-in-table))
|
(assert-equal 4 (length pairs-in-table))
|
||||||
(true-or-false? ____ (find '("The Hobbit" "Bilbo") pairs-in-table
|
(true-or-false? 0 (find '("The Hobbit" "Bilbo") pairs-in-table
|
||||||
:test #'equal)))))
|
:test #'equal)))))
|
||||||
|
|
||||||
(define-test loop-statistics
|
(define-test loop-statistics
|
||||||
|
@ -110,31 +111,31 @@
|
||||||
finally (return (list collected counted summed
|
finally (return (list collected counted summed
|
||||||
maximized minimized)))))
|
maximized minimized)))))
|
||||||
(destructuring-bind (collected counted summed maximized minimized) result
|
(destructuring-bind (collected counted summed maximized minimized) result
|
||||||
(assert-equal ____ collected)
|
(assert-equal '(1 2 4 8 16 32) collected)
|
||||||
(assert-equal ____ counted)
|
(assert-equal 6 counted)
|
||||||
(assert-equal ____ summed)
|
(assert-equal 63 summed)
|
||||||
(assert-equal ____ maximized)
|
(assert-equal 32 maximized)
|
||||||
(assert-equal ____ minimized))))
|
(assert-equal 1 minimized))))
|
||||||
|
|
||||||
(define-test loop-destructuring
|
(define-test loop-destructuring
|
||||||
;; LOOP can bind multiple variables on each iteration step.
|
;; LOOP can bind multiple variables on each iteration step.
|
||||||
(let* ((count 0)
|
(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)
|
do (incf count)
|
||||||
collect (+ a b))))
|
collect (+ a b))))
|
||||||
(assert-equal ____ count)
|
(assert-equal 4 count)
|
||||||
(assert-equal ____ result)))
|
(assert-equal '(10 10 10 10) result)))
|
||||||
|
|
||||||
(define-test loop-conditional-execution
|
(define-test loop-conditional-execution
|
||||||
(let ((numbers '(1 1 2 3 5 8 13 21)))
|
(let ((numbers '(1 1 2 3 5 8 13 21)))
|
||||||
;; LOOP can execute some actions conditionally.
|
;; LOOP can execute some actions conditionally.
|
||||||
(let ((result (loop for x in numbers
|
(let ((result (loop for x in numbers
|
||||||
when (evenp x) sum x)))
|
when (evenp x) sum x)))
|
||||||
(assert-equal ____ result))
|
(assert-equal 10 result))
|
||||||
(let ((result (loop for x in numbers
|
(let ((result (loop for x in numbers
|
||||||
unless (evenp x) sum x)))
|
unless (evenp x) sum x)))
|
||||||
(assert-equal ____ result))
|
(assert-equal 44 result))
|
||||||
(flet ((greater-than-10-p (x) (> x 10)))
|
(flet ((greater-than-10-p (x) (> x 10)))
|
||||||
(let ((result (loop for x in numbers
|
(let ((result (loop for x in numbers
|
||||||
when (greater-than-10-p x) sum x)))
|
when (greater-than-10-p x) sum x)))
|
||||||
(assert-equal ____ result)))))
|
(assert-equal 34 result)))))
|
||||||
|
|
|
@ -49,8 +49,54 @@
|
||||||
;;;
|
;;;
|
||||||
;;; Your goal is to write the scoring function for Greed.
|
;;; 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
|
(define-test score-of-an-empty-list-is-zero
|
||||||
(assert-equal 0 (score)))
|
(assert-equal 0 (score)))
|
||||||
|
|
Loading…
Reference in New Issue