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

vectors.lsp 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. "vectors are just like rank 1 arrays"
  15. (define-test test-vector-types
  16. " #(x y z) defines a vector literal containing x y z"
  17. (true-or-false? t (typep #(1 11 111) 'vector))
  18. (assert-equal 11 (aref #(1 11 111) 1)))
  19. (define-test test-length-works-on-vectors
  20. (assert-equal (length #(1 2 3)) 3 ))
  21. (define-test test-bit-vector
  22. "#*0011 defines a bit vector literal with four elements, 0, 0, 1 and 1"
  23. (assert-equal #*0011 (make-array '4 :element-type 'bit :initial-contents '(0 0 1 1)))
  24. (true-or-false? t (typep #*1001 'bit-vector))
  25. (assert-equal 0 (aref #*1001 1)))
  26. (define-test test-some-bitwise-operations
  27. (assert-equal #*1000 (bit-and #*1100 #*1010))
  28. (assert-equal #*1110 (bit-ior #*1100 #*1010))
  29. (assert-equal #*0110 (bit-xor #*1100 #*1010)))
  30. (defun list-to-bit-vector (my-list)
  31. (make-array (length my-list) :element-type 'bit :initial-contents my-list))
  32. (define-test test-list-to-bit-vector
  33. "you must complete list-to-bit-vector"
  34. (assert-true (typep (list-to-bit-vector '(0 0 1 1 0)) 'bit-vector))
  35. (assert-equal (aref (list-to-bit-vector '(0)) 0) 0)
  36. (assert-equal (aref (list-to-bit-vector '(0 1)) 1) 1)
  37. (assert-equal (length (list-to-bit-vector '(0 0 1 1 0 0 1 1))) 8))