Fork of https://github.com/google/lisp-koans so that I could go through them. THIS CONTAINS ANSWERS.

arrays.lsp 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ;; Copyright 2013 Google Inc.
  2. ;;
  3. ;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;; you may not use this file except in compliance with the License.
  5. ;; You may obtain a copy of the License at
  6. ;;
  7. ;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;
  9. ;; Unless required by applicable law or agreed to in writing, software
  10. ;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;; See the License for the specific language governing permissions and
  13. ;; limitations under the License.
  14. ;; see http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node157.html
  15. (define-test test-basic-array-stuff
  16. " the first block of code defines an 8x8 array, then fills
  17. the elements with a checkerboard pattern"
  18. (let ((chess-board))
  19. (setf chess-board (make-array '(8 8)))
  20. "this dotimes is an iterator which loops x over integers 0 to 7"
  21. (dotimes (x 8)
  22. (dotimes (y 8)
  23. (if (evenp (+ x y))
  24. (setf (aref chess-board x y) :black)
  25. (setf (aref chess-board x y) :white))))
  26. (assert-true (typep chess-board 'array))
  27. (assert-equal (aref chess-board 0 0) :black)
  28. (assert-equal (aref chess-board 2 3) :white)
  29. "array-rank returns the number of dimensions of the array"
  30. (assert-equal 2 (array-rank chess-board))
  31. "array-dimensions returns a list of the cardinality of the array dims"
  32. (assert-equal '(8 8) (array-dimensions chess-board))
  33. (assert-equal 64 (array-total-size chess-board))))
  34. (define-test test-make-your-own-array
  35. "make your own array that meets the specifications below."
  36. (let ((color-cube (make-array '(3 3 3))))
  37. (setf (aref color-cube 0 1 2) :red)
  38. (setf (aref color-cube 2 1 0) :white)
  39. "you may need to modify your array after you make it"
  40. (if (typep color-cube '(simple-array T (3 3 3)))
  41. (progn
  42. (assert-equal 3 (array-rank color-cube))
  43. (assert-equal '(3 3 3) (array-dimensions color-cube))
  44. (assert-equal 27 (array-total-size color-cube))
  45. (assert-equal (aref color-cube 0 1 2) :red)
  46. (assert-equal (aref color-cube 2 1 0) :white))
  47. (assert-true nil))))
  48. (define-test test-adjustable-array
  49. "one may build arrays that can change size"
  50. (let ((x (make-array '(2 2) :initial-element 5 :adjustable t)))
  51. (assert-equal (aref x 1 0) 5)
  52. (assert-equal (array-dimensions x) '(2 2))
  53. (adjust-array x '(3 4))
  54. (assert-equal (array-dimensions x) '(3 4))))
  55. (define-test test-make-array-from-list
  56. (let ((x))
  57. (setf x (make-array '(4) :initial-contents '(:one :two :three :four)))
  58. (assert-equal (array-dimensions x) '(4))
  59. (assert-equal :one (aref x 0))))
  60. (define-test test-row-major-index
  61. "row major indexing is a way to access elements with a single integer,
  62. rather than a list of integers"
  63. (let ((my-array nil))
  64. (setf my-array (make-array '(2 2 2 2)))
  65. (dotimes (i (* 2 2 2 2))
  66. (setf (row-major-aref my-array i) i))
  67. (assert-equal (aref my-array 0 0 0 0) 0)
  68. (assert-equal (aref my-array 1 1 1 1) 15)))