-
Notifications
You must be signed in to change notification settings - Fork 566
/
Copy pathvectors.lisp
53 lines (44 loc) · 2.11 KB
/
vectors.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
;;; Copyright 2013 Google Inc.
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
;;; Vectors are one-dimensional arrays. This means that general array operations
;;; will work on vectors normally. However, Lisp also defines some functions for
;;; operating on sequences - which means, either vectors or lists.
(define-test vector-basics
;; #(...) is syntax sugar for defining literal vectors.
(let ((vector #(1 11 111)))
(true-or-false? ____ (typep vector 'vector))
(assert-equal ____ (aref vector 1))))
(define-test length
;; The function LENGTH works both for vectors and for lists.
(assert-equal ____ (length '(1 2 3)))
(assert-equal ____ (length #(1 2 3))))
(define-test bit-vector
;; #*0011 defines a bit vector literal with four elements: 0, 0, 1 and 1.
(assert-equal #*0011 (make-array 4 :element-type 'bit :initial-contents ____))
(true-or-false? ____ (typep #*1001 'bit-vector))
(assert-equal ____ (aref #*1001 1)))
(define-test bitwise-operations
;; Lisp defines a few bitwise operations that work on bit vectors.
(assert-equal ____ (bit-and #*1100 #*1010))
(assert-equal ____ (bit-ior #*1100 #*1010))
(assert-equal ____ (bit-xor #*1100 #*1010)))
(defun list-to-bit-vector (list)
;; Implement a function that turns a list into a bit vector.
____)
(define-test list-to-bit-vector
;; You need to fill in the blank in LIST-TO-BIT-VECTOR.
(assert-true (typep (list-to-bit-vector '(0 0 1 1 0)) 'bit-vector))
(assert-equal (aref (list-to-bit-vector '(0)) 0) 0)
(assert-equal (aref (list-to-bit-vector '(0 1)) 1) 1)
(assert-equal (length (list-to-bit-vector '(0 0 1 1 0 0 1 1))) 8))