cool about arrays and vectors

arrays have #'array-dimentions and #'array-rank (as a matrix)
and there's cool (row-major-aref my-arr index) for indexing array with
single index, going throug all elements

vectors have literal notation #(1 2 4 1),
they are one dimentional
have :element-type key in constructor, not quite sure how to use that

and we can try to #'coerce list to something else by providing symbol
for type, which we could get from (type-of my-vector)

also literal notation for byte vectors, cool #*110011001
This commit is contained in:
efim 2022-07-26 12:18:42 +00:00
parent ce9c7ac999
commit 6dcca61283
2 changed files with 31 additions and 28 deletions

View File

@ -21,22 +21,22 @@
;; AREF stands for "array reference". ;; AREF stands for "array reference".
(setf (aref chess-board x y) (if (evenp (+ x y)) :black :white)))) (setf (aref chess-board x y) (if (evenp (+ x y)) :black :white))))
(assert-true (typep chess-board 'array)) (assert-true (typep chess-board 'array))
(assert-equal ____ (aref chess-board 0 0)) (assert-equal :black (aref chess-board 0 0))
(assert-equal ____ (aref chess-board 2 3)) (assert-equal :white (aref chess-board 2 3))
;; The function ARRAY-RANK returns the number of dimensions of the array. ;; The function ARRAY-RANK returns the number of dimensions of the array.
(assert-equal ____ (array-rank chess-board)) (assert-equal 2 (array-rank chess-board))
;; The function ARRAY-DIMENSIONS returns a list of the cardinality of the ;; The function ARRAY-DIMENSIONS returns a list of the cardinality of the
;; array dimensions. ;; array dimensions.
(assert-equal ____ (array-dimensions chess-board)) (assert-equal '(8 8) (array-dimensions chess-board))
;; ARRAY-TOTAL-SIZE returns the total number of elements in the array. ;; ARRAY-TOTAL-SIZE returns the total number of elements in the array.
(assert-equal ____ (array-total-size chess-board)))) (assert-equal (* 8 8) (array-total-size chess-board))))
(define-test make-your-own-array (define-test make-your-own-array
;; Make your own array that satisfies the test. ;; Make your own array that satisfies the test.
(let ((color-cube ____)) (let ((color-cube (make-array '(3 3 3))))
;; You may need to modify your array after you create it. ;; You may need to modify your array after you create it.
(setf (____ color-cube ____ ____ ____) ____ (setf (aref color-cube 0 1 2) :red
(____ color-cube ____ ____ ____) ____) (aref color-cube 2 1 0) :white)
(if (typep color-cube '(simple-array t (3 3 3))) (if (typep color-cube '(simple-array t (3 3 3)))
(progn (progn
(assert-equal 3 (array-rank color-cube)) (assert-equal 3 (array-rank color-cube))
@ -49,16 +49,16 @@
(define-test adjustable-array (define-test adjustable-array
;; The size of an array does not need to be constant. ;; The size of an array does not need to be constant.
(let ((x (make-array '(2 2) :initial-element 5 :adjustable t))) (let ((x (make-array '(2 2) :initial-element 5 :adjustable t)))
(assert-equal ____ (aref x 1 0)) (assert-equal 5 (aref x 1 0))
(assert-equal ____ (array-dimensions x)) (assert-equal '(2 2) (array-dimensions x))
(adjust-array x '(3 4)) (adjust-array x '(3 4))
(assert-equal ____ (array-dimensions x)))) (assert-equal '(3 4) (array-dimensions x))))
(define-test make-array-from-list (define-test make-array-from-list
;; One can create arrays with initial contents. ;; One can create arrays with initial contents.
(let ((x (make-array '(4) :initial-contents '(:one :two :three :four)))) (let ((x (make-array '(4) :initial-contents '(:one :two :three :four))))
(assert-equal ____ (array-dimensions x)) (assert-equal '(4) (array-dimensions x))
(assert-equal ____ (aref x 0)))) (assert-equal :one (aref x 0))))
(define-test row-major-index (define-test row-major-index
;; Row major indexing is a way to access elements with a single integer, ;; Row major indexing is a way to access elements with a single integer,
@ -66,7 +66,7 @@
(let ((my-array (make-array '(2 2 2 2)))) (let ((my-array (make-array '(2 2 2 2))))
(dotimes (i (* 2 2 2 2)) (dotimes (i (* 2 2 2 2))
(setf (row-major-aref my-array i) i)) (setf (row-major-aref my-array i) i))
(assert-equal ____ (aref my-array 0 0 0 0)) (assert-equal 0 (aref my-array 0 0 0 0))
(assert-equal ____ (aref my-array 0 0 1 0)) (assert-equal 2 (aref my-array 0 0 1 0))
(assert-equal ____ (aref my-array 0 1 0 0)) (assert-equal 4 (aref my-array 0 1 0 0))
(assert-equal ____ (aref my-array 1 1 1 1)))) (assert-equal 15 (aref my-array 1 1 1 1))))

View File

@ -19,29 +19,32 @@
(define-test vector-basics (define-test vector-basics
;; #(...) is syntax sugar for defining literal vectors. ;; #(...) is syntax sugar for defining literal vectors.
(let ((vector #(1 11 111))) (let ((vector #(1 11 111)))
(true-or-false? ____ (typep vector 'vector)) (true-or-false? t (typep vector 'vector))
(assert-equal ____ (aref vector 1)))) (assert-equal 11 (aref vector 1))))
(define-test length (define-test length
;; The function LENGTH works both for vectors and for lists. ;; The function LENGTH works both for vectors and for lists.
(assert-equal ____ (length '(1 2 3))) (assert-equal 3 (length '(1 2 3)))
(assert-equal ____ (length #(1 2 3)))) (assert-equal 3 (length #(1 2 3))))
(define-test bit-vector (define-test bit-vector
;; #*0011 defines a bit vector literal with four elements: 0, 0, 1 and 1. ;; #*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 ____)) (assert-equal #*0011 (make-array 4 :element-type 'bit :initial-contents '(0 0 1 1 )))
(true-or-false? ____ (typep #*1001 'bit-vector)) (true-or-false? t (typep #*1001 'bit-vector))
(assert-equal ____ (aref #*1001 1))) (assert-equal 0 (aref #*1001 1)))
(define-test bitwise-operations (define-test bitwise-operations
;; Lisp defines a few bitwise operations that work on bit vectors. ;; Lisp defines a few bitwise operations that work on bit vectors.
(assert-equal ____ (bit-and #*1100 #*1010)) (assert-equal #*1000 (bit-and #*1100 #*1010))
(assert-equal ____ (bit-ior #*1100 #*1010)) (assert-equal #*1110 (bit-ior #*1100 #*1010)) ; use sly-documentation-lookup ; that's an INCLUSIVE-OR
(assert-equal ____ (bit-xor #*1100 #*1010))) (assert-equal #*0110 (bit-xor #*1100 #*1010))) ; that one I recognised, and EXCLUSIVE-OR
(type-of #*110011)
(defun list-to-bit-vector (list) (defun list-to-bit-vector (list)
;; Implement a function that turns a list into a bit vector. ;; Implement a function that turns a list into a bit vector.
____) ;; (vector :element-type 'bit :initial-contents list) ; my bad solution
(coerce list 'bit-vector) ; so, trying to take
)
(define-test list-to-bit-vector (define-test list-to-bit-vector
;; You need to fill in the blank in LIST-TO-BIT-VECTOR. ;; You need to fill in the blank in LIST-TO-BIT-VECTOR.