Version 1, March 1993
This design note adds a specification for the generic function last-setter, consistent with the setters for first, second, third, and element.
Specify the generic function last-setter, as follows:
last-setter mutable-sequence new-value => new-value [Generic Function]Replaces the last element of mutable-sequence with new-value. new-value must obey any type restrictions for elements of mutable-sequence. An error is signaled if mutable-sequence is empty or unbounded.
? (define my-list (list 1 2 3)) ;unspecified ? my-list 1 2 3 ? (set! (last my-list) 4) 4 ? my-list 1 2 4 ? (define my-empty-vector (vector)) ;unspecified ? my-empty-vector #() ? (set! (last my-empty-vector) 0) ;error
The following is a portable implementation:
(define-method last-setter ((s <mutable-sequence>) new) (case (size s) ((0 #f) ;check for empty or unbounded sequence (error "last-setter of empty sequence ~S" s)) (else: (bind ((first-state (initial-state s))) (bind-methods ((loop (trail advance) (bind ((next (next-state s advance)) (trail (next-state s trail))) (if next (loop trail next) (set! (current-element s trail) new))))) (loop (initial-state s) (next-state s first-state)))))))
Next chapter: #12: Size-Setter for Stretchy Sequences (Addition)