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

vectors.lsp 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 ___ (typep #(1 11 111) 'vector))
  18. (assert-equal ___ (aref #(1 11 111) 1)))
  19. (define-test test-length-works-on-vectors
  20. (assert-equal (length #(1 2 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))
  24. (true-or-false? ____ (typep #*1001 'bit-vector))
  25. (assert-equal ____ (aref #*1001 1)))
  26. (define-test test-some-bitwise-operations
  27. (assert-equal ___ (bit-and #*1100 #*1010))
  28. (assert-equal ___ (bit-ior #*1100 #*1010))
  29. (assert-equal ___ (bit-xor #*1100 #*1010)))
  30. (defun list-to-bit-vector (my-list)
  31. nil)
  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))