example.scm 907 B

1234567891011121314151617181920212223242526
  1. (use-modules (srfi srfi-1))
  2. (define (make-dumbhash size)
  3. "Make a dumb hash table: an array of buckets"
  4. (make-array '() size))
  5. (define* (dumbhash-ref dumbhash key #:optional (default #f))
  6. "Pull a value out of a dumbhash"
  7. (let* ((hashed-key (hash key (array-length dumbhash)))
  8. (bucket (array-ref dumbhash hashed-key)))
  9. (or (find (lambda (x) (equal? (car x) key))
  10. bucket)
  11. default)))
  12. (define (dumbhash-set! dumbhash key val)
  13. "Set a value in a dumbhash"
  14. (let* ((hashed-key (hash key (array-length dumbhash)))
  15. (bucket (array-ref dumbhash hashed-key)))
  16. ;; Only act if it's not already a member
  17. (if (not (find (lambda (x) (equal? (car x) key))
  18. bucket))
  19. (array-set! dumbhash
  20. ;; extend the bucket with the key-val pair
  21. (cons (cons key val) bucket)
  22. hashed-key))))