unbalanced-set.scm 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. (library (unbalanced-set)
  2. (export make-empty-set
  3. set-insert
  4. set-empty?
  5. set-member?)
  6. (import (except (rnrs base) error vector-map)
  7. (only (guile)
  8. lambda*
  9. λ)
  10. ;; SRFI-9 for structs
  11. (srfi srfi-9 gnu)
  12. ;; let-values
  13. (srfi srfi-11)
  14. (ice-9 exceptions)
  15. (binary-tree))
  16. ;; Maybe it would be appropriate to wrap the unbalanced binary
  17. ;; search tree in a record, to have predicates and be able to store
  18. ;; the less operation.
  19. (define-immutable-record-type <unbalanced-set>
  20. (construct-empty-set items less)
  21. set?
  22. (items set-items set-set-items)
  23. (less set-less set-set-less))
  24. (define make-empty-set
  25. (λ (less)
  26. (construct-empty-set (make-empty-tree) less)))
  27. (define set-empty?
  28. (λ (set)
  29. (tree-empty? (set-items set))))
  30. (define set-member?
  31. (λ (set elem)
  32. (cond
  33. [(set-empty? set) #f]
  34. [else
  35. (tree-member? (set-items set)
  36. elem
  37. (set-less set))])))
  38. (define set-insert
  39. (λ (set elem)
  40. ;; note: copying the set struct here, which could be avoided by
  41. ;; not using a struct
  42. (set-set-items set
  43. (tree-insert (set-items set)
  44. elem
  45. (set-less set))))))