example.scm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ;; =================================
  2. ;; Creating arrays using expressions
  3. ;; =================================
  4. ;; define a 2D array ...
  5. #2()
  6. ;; ... of unsigned integers of 8 bit length ...
  7. #2u8()
  8. ;; ... of specified lengths, needing intial values ...
  9. #2u8:4:4((0 0 0 0)
  10. (0 0 0 0)
  11. (0 0 0 0)
  12. (0 0 0 0))
  13. ;; ... of specified indice starts
  14. #2u8@2:4@4:4((0 0 0 0)
  15. (0 0 0 0)
  16. (0 0 0 0)
  17. (0 0 0 0))
  18. ;; ================================
  19. ;; Creating arrays using procedures
  20. ;; ================================
  21. ;; specifying the number of elements inside the array with an initial value
  22. ;; (make-array fill bounds ...)
  23. (make-array 'ho 2 3) ; --> #2((ho ho ho) (ho ho ho))
  24. ;; specifying ranges of the dimensions
  25. ;; '(0 1) will contain 2 items.
  26. ;; '(0 2) is the range from 0 to 2 inclusive, and thus is 3 items.
  27. (make-array 'ho '(0 1) '(0 2)) ; #2((ho ho ho) (ho ho ho))
  28. ;; making a typed array of type u8 filled with 255 and size 2x2:
  29. (make-typed-array 'u8 255 2 2)
  30. ;; but not this ...
  31. (make-typed-array 'u8 256 2 2)
  32. ;; --> ERROR: In procedure make-srfi-4-vector:
  33. ;; In procedure bytevector-u8-set!: Value out of range: 256
  34. ;; ==========
  35. ;; Predicates
  36. ;; ==========
  37. (array? #2u8:4:4((0 0 0 0)
  38. (0 0 0 0)
  39. (0 0 0 0)
  40. (0 0 0 0))) ; #t
  41. (array? '()) ; #f
  42. (typed-array? #2u8:4:4((0 0 0 0)
  43. (0 0 0 0)
  44. (0 0 0 0)
  45. (0 0 0 0))
  46. 'u8) ; #t
  47. (typed-array? #2:4:4((0 0 0 0)
  48. (0 0 0 0)
  49. (0 0 0 0)
  50. (0 0 0 0))
  51. 'u8) ; #f
  52. ;; ===========
  53. ;; Referencing
  54. ;; ===========
  55. (array-ref
  56. #2:4:4((0 1 2 3)
  57. (4 5 6 7)
  58. (8 9 10 11)
  59. (12 13 14 15))
  60. 3 3)
  61. ;; continue: https://www.gnu.org/software/guile/manual/html_node/Array-Procedures.html#Array-Procedures