hashtable-ref(3scm)

πŸ“¦ version 0-unstable-2024-02, in nixpkgs unstable

πŸ’– hashtable-ref β€” get a value from a hashtable πŸ’–

okay so this is hashtable-ref(3scm) straight out of the Scheme Programmer's Manual, and honestly? she's a little icon. tiny function, huge main character energy. let's get into it 🫢


✨ LIBRARY ✨

like, pick your import, bestie β€” both work:

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

✨ SYNOPSIS ✨

(hashtable-ref hashtable key default)

✨ DESCRIPTION ✨

So basically πŸ’…: this returns the value in hashtable that's associated with key. that's it, that's the vibe. but lowkey the important part β€” if the hashtable does not contain an association for key, then you just get default back. no drama, no error, she just hands you the fallback. πŸŽ€

✨ RETURN VALUES ✨

returns a single object, periodt β€” either a value from an association in hashtable, or the object default. one object, no more no less πŸ¦‹

✨ EXAMPLES ✨

ok watch this, it's giving textbook:

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

see how 'foo hits and gives you bar (slay ✨), but 'bar isn't in there so you get the #f default. iconic behaviour, no notes πŸ’œ

✨ COMPATIBILITY ✨

ngl this procedure is unique to R6RS 🚨. similar procedures do exist in SRFI-69 and SRFI-125, so if you're coming from one of those, don't assume they're identical β€” iykyk.

✨ ERRORS ✨

this one can raise exceptions with these condition types:

  • &assertion (R6RS) β€” you passed the wrong number of arguments, 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. so like… vibe check your arguments first, bestie 😭

✨ SEE ALSO ✨

go read these next, fr fr:

✨ STANDARDS ✨

R6RS πŸ‘‘

✨ HISTORY ✨

this procedure first appeared in R6RS. and plot twist πŸ“ β€” an equivalent procedure already existed in SRFI-69 under the name hash-table-ref. so she's not new-new, she's just R6RS-branded.

✨ AUTHORS ✨

this page is part of the scheme-manpages project πŸ’– it includes materials from the RnRS documents. more info can be found at https://github.com/schemedoc/manpages/.

✨ BUGS ✨

ok this is the part where we pay attention, because this one bites πŸ™ˆ

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 ...))

like… if #f is a legit value in your table, you literally cannot tell "found and it's #f" apart from "not found". cursed. πŸ’€

use hashtable-contains?(3scm) to get around this problem. that's the whole fix, go forth and be safe 🧚


and that's the page!! 🌈 brought to you by the scheme-manpages source, version dated 2023-01-22, filed under hashtable-ref(3scm) β€” mwah, love u, go look up your keys responsibly πŸ’…βœ¨πŸŒΈ

πŸ“¦ where this page comes from