Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; same as (list-set! l elt val)
- ;; however it works even if
- ;; - l isn't a list - defaults to '()
- ;; - l is too short for elt - will extend l with filler #f elements
- (define (list-set-safe! l elt val)
- (let ((lst (if (list? l) l (list))))
- (if (> (length lst) elt)
- (list-set! lst elt val)
- (append! lst
- (make-list (- elt (length lst)) #f)
- (list val)))
- lst))
- (define (test-list-set-safe)
- (define l #f)
- (list-set-safe! l 1 9)
- (test-equal "list-set-safe 1 to 9"
- (list #f 9)
- l)
- (set! l (list 1 2))
- (list-set-safe! l 0 9)
- (test-equal "list-set-safe 0 to 9"
- (list 9 2)
- l)
- (list-set-safe! l 1 9)
- (test-equal "list-set-safe 1 to 9"
- (list 9 9)
- l)
- (list-set-safe! l 2 9)
- (test-equal "list-set-safe 2 to 9"
- (list 9 9 9)
- l)
- (list-set-safe! l 4 9)
- (test-equal "list-set-safe 4 to 9"
- (list 9 9 9 #f 9)
- l)
- (test-error
- "list-set-safe -1 to 9"
- 'out-of-range
- (list-set-safe! l -1 9)))
Add Comment
Please, Sign In to add comment