tagged-data.scm 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. (library (tagged-data)
  2. (export type-tag
  3. contents
  4. attach-tag)
  5. (import (except (rnrs base)
  6. error
  7. map)
  8. (only (guile)
  9. lambda* λ
  10. simple-format)
  11. (ice-9 exceptions)
  12. (custom-exceptions))
  13. ;;; data abstraction layer over tagged data
  14. ;;; ACCESSORS for type tag
  15. (define type-tag
  16. (λ (datum)
  17. (if (pair? datum)
  18. (car datum)
  19. (raise-exception
  20. (make-exception
  21. (make-inappropriate-value-exception datum)
  22. (make-exception-with-message "datum must be a tagged datum, a pair")
  23. (make-exception-with-irritants (list datum))
  24. (make-exception-with-origin 'type-tag))))))
  25. (define contents
  26. (λ (datum)
  27. (simple-format #t "in contents: datum: ~a\n" datum)
  28. (if (pair? datum)
  29. (cdr datum)
  30. (raise-exception
  31. (make-exception
  32. (make-inappropriate-value-exception datum)
  33. (make-exception-with-message "datum must be a pair of tag and content")
  34. (make-exception-with-irritants (list datum))
  35. (make-exception-with-origin 'contents))))))
  36. ;;; SETTER for type tag
  37. (define attach-tag
  38. (λ (tag contents)
  39. (cons tag contents))))