hashtable-ref - get a value from a hashtable

LIBRARY

(import (rnrs))                     ;R6RS
(import (rnrs hashtables))          ;R6RS

SYNOPSIS

(hashtable-ref hashtable key default)

DESCRIPTION

Returns the value in hashtable associated with key. If hashtable does not contain an association for key, default is returned.

RETURN VALUES

Returns a single object; either a value from an association in hashtable or the object default.

EXAMPLES

(define ht (make-eq-hashtable))
(hashtable-set! ht 'foo 'bar)
(hashtable-ref ht 'foo #f)   =>  bar
(hashtable-ref ht 'bar #f)   =>  #f

COMPATIBILITY

This procedure is unique to R6RS. Similar procedures exist in SRFI-69 and SRFI-125.

ERRORS

This procedure can raise exceptions with the following condition types:
&assertion (R6RS)
The wrong number of arguments was passed or an argument was outside its domain. In particular, hashtable should be a hashtable and key should be in the domain of the hashtable's hash and equal functions.

SEE ALSO

make-hashtable(3scm), hashtable-keys(3scm), hashtable-contains?(3scm)

STANDARDS

R6RS

HISTORY

This procedure first appeared in R6RS. An equivalent procedure already existed in SRFI-69 under the name hash-table-ref.

AUTHORS

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://github.com/schemedoc/manpages/.

BUGS

The following code pattern does not work if the hashtable's keys can be associated with the false object.

(cond ((hashtable-ref ht key #f) =>
       (lambda (value)
         (... do something with value ...)))
      (else
       ... assume key is not in ht ...))

Use hashtable-contains?(3scm) to get around this problem.


Markup created by unroff 1.0sc,    March 04, 2023.