MC logo

Simple Array Demo


CS 231 Lecture Examples

<< Download >>
; An array.  Subscript from 0.
#(a b c)

; Access to an array.
(aref #(a b c d e f) 2)

; Setting an array element then discarding the array.
(setf (aref #(a b c d e f) 2) 'q)

; Here's a more likely use.
(setq arr #(a b c d e f))
arr
(aref arr 3)
(setf (aref arr 2) 77)
arr

;
; Reverse the array in place.
(defun rev-array (arr)
    (if (and (arrayp arr) (= (array-rank arr) 1))
        ; Do the actual work.
        (rev-array-r arr 0 (- (array-dimension arr 0) 1))

        ; Wrong type of arg
        (error "rev-array needs a one-dimensional array.")
    )
)
(defun rev-array-r (arr low high)
    (if (>= low high)
        arr
        (let*
            ((first (aref arr low)))
            (progn
                (setf (aref arr low) (aref arr high))
                (setf (aref arr high) first)
                (rev-array-r arr (+ low 1) (- high 1))
            )
        )
    )
)

<<
>>