Advertisement
Guest User

Untitled

a guest
Dec 1st, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1.  
  2. (defpackage "COM.INFORMATIMAGO.COMMON-LISP.CARRAY"
  3. (:use "CL")
  4. (:shadow "MAKE-ARRAY" "AREF" "ARRAY-DIMENSIONS" #|…|#)
  5. (:export "MAKE-ARRAY" "AREF" "ARRAY-DIMENSIONS" #|…|#))
  6.  
  7. (in-package "COM.INFORMATIMAGO.COMMON-LISP.CARRAY")
  8.  
  9. (defstruct (controlled-array
  10. (:constructor make-controlled-array (array element-type))
  11. (:conc-name %carray-))
  12. array
  13. element-type)
  14.  
  15. (defun make-array (dimensions &rest keys &key (element-type 't) &allow-other-keys)
  16. (make-controlled-array (apply (function cl:make-array) dimensions keys)
  17. element-type))
  18.  
  19. (defun aref (array &rest indices)
  20. (apply (function cl:aref) (%carray-array array) indices))
  21.  
  22. (defun (setf aref) (new-value array &rest indices)
  23. (if (typep new-value (%carray-element-type array))
  24. (setf (apply (function cl:aref) (%carray-array array) indices) new-value)
  25. (error 'type-error :datum new-value :expected-type (%carray-element-type array))))
  26.  
  27. (defun array-dimensions (array)
  28. (cl:array-dimensions array))
  29.  
  30.  
  31.  
  32. (let ((array (make-array 3
  33. :element-type '(integer 1 12)
  34. :initial-element 6)))
  35. (print (multiple-value-list (ignore-errors (setf (aref array 2) 100))))
  36. (print array)
  37. (setf (aref array 1) 7)
  38. (print array)
  39. (values))
  40.  
  41.  
  42. ;; prints:
  43. ;; (nil #<type-error #x30203DC8ABDD>)
  44. ;; #S(controlled-array :array #(6 6 6) :element-type (integer 1 12))
  45. ;; #S(controlled-array :array #(6 7 6) :element-type (integer 1 12))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement