example.scm 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. ;; SRFI 9: record types
  2. (use-modules
  3. ;; structs
  4. (srfi srfi-9)
  5. ;; for functional structs (not part of srfi-9 directly)
  6. (srfi srfi-9 gnu))
  7. ;; ==============
  8. ;; NORMAL STRUCTS
  9. ;; ==============
  10. (define-record-type <bitboard>
  11. ;; define constructor
  12. (make-bitboard bits height width kind)
  13. ;; define predicate
  14. bitboard?
  15. ;; define accessors
  16. (bits bitboard-bits)
  17. (height bitboard-height)
  18. (width bitboard-width)
  19. (kind bitboard-kind))
  20. (set-record-type-printer!
  21. <bitboard>
  22. (λ (record port)
  23. (display
  24. (simple-format port
  25. "Bitbard: <bits: ~a, height: ~a, width: ~a, kind: ~s>\n"
  26. (bitboard-bits record)
  27. (bitboard-height record)
  28. (bitboard-width record)
  29. (bitboard-kind record)))))
  30. ;; ==================
  31. ;; FUNCTIONAL STRUCTS
  32. ;; ==================
  33. ;; "[...] the define-immutable-record-type works like define-record-type, except that fields are immutable and setters are defined as functional setters."
  34. (define-immutable-record-type <bitboard>
  35. ;; define constructor
  36. (make-bitboard bits height width kind)
  37. ;; define predicate
  38. bitboard?
  39. ;; define accessors and functional setters
  40. (bits bitboard-bits set-bitboard-bits)
  41. (height bitboard-height set-bitboard-height)
  42. (width bitboard-width set-bitboard-width)
  43. (kind bitboard-kind set-bitboard-kind))
  44. ;; =====
  45. ;; USAGE
  46. ;; =====
  47. (let ([height 9]
  48. [width 9])
  49. (let ([num-bits (* height width)])
  50. (display (make-bitboard (make-bitvector num-bits 'P) 9 9 'shogi))))