;;-----------------------------
;; Scheme does not normally grow and shrink arrays in the way that
;; Perl can. The more usual operations are adding and removing from
;; the head of a list using the `cons' and `cdr' procedures.
;; However ...
(define(grow/shrink list new-size)(let((size (length list)))(cond((< size new-size)(grow/shrink (cons "" list) new-size))((> size new-size)(grow/shrink (cdr list) new-size))(else list))))(define(element list i)(list-ref list (- (length list) i 1)))(define(set-element list i value)(if(>= i (length list))(set! list (grow/shrink list (- i 1))))(set-car! (list-cdr-ref list (- (length list) i 1)))
list)(define(what-about list)(let((len (length list)))(format #t "The array now has ~A elements.\n" len)(format #t "The index of the last element is ~A.\n"(- len 1))(format #t "Element #3 is `~A'.\n"(if(> len 3)(element list 3)""))));; In the emulation of Perl arrays implemented here, the elements are
;; in reverse order when compared to normal Scheme lists.
(definepeople(reverse '("Crosby""Stills""Nash""Young")))(what-about people);;-----------------------------
;; The array now has 4 elements.
;; The index of the last element is 3.
;; Element #3 is `Young'.
;;-----------------------------
(set! people (grow/shrink people 3))(what-about people);;-----------------------------
;; The array now has 3 elements.
;; The index of the last element is 2.
;; Element #3 is `'.
;;-----------------------------
(set! people (grow/shrink people 10001))(what-about people);;-----------------------------
;; The array now has 10001 elements.
;; The index of the last element is 10000.
;; Element #3 is `'.
;;-----------------------------