Guest User

Untitled

a guest
Jun 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. ;; same as (list-set! l elt val)
  2. ;; however it works even if
  3. ;; - l isn't a list - defaults to '()
  4. ;; - l is too short for elt - will extend l with filler #f elements
  5. (define (list-set-safe! l elt val)
  6. (let ((lst (if (list? l) l (list))))
  7. (if (> (length lst) elt)
  8. (list-set! lst elt val)
  9. (append! lst
  10. (make-list (- elt (length lst)) #f)
  11. (list val)))
  12. lst))
  13.  
  14. (define (test-list-set-safe)
  15. (define l #f)
  16. (list-set-safe! l 1 9)
  17. (test-equal "list-set-safe 1 to 9"
  18. (list #f 9)
  19. l)
  20. (set! l (list 1 2))
  21. (list-set-safe! l 0 9)
  22. (test-equal "list-set-safe 0 to 9"
  23. (list 9 2)
  24. l)
  25. (list-set-safe! l 1 9)
  26. (test-equal "list-set-safe 1 to 9"
  27. (list 9 9)
  28. l)
  29. (list-set-safe! l 2 9)
  30. (test-equal "list-set-safe 2 to 9"
  31. (list 9 9 9)
  32. l)
  33. (list-set-safe! l 4 9)
  34. (test-equal "list-set-safe 4 to 9"
  35. (list 9 9 9 #f 9)
  36. l)
  37. (test-error
  38. "list-set-safe -1 to 9"
  39. 'out-of-range
  40. (list-set-safe! l -1 9)))
Add Comment
Please, Sign In to add comment