hashtable-ref(3scm)
π 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:
make-hashtable(3scm)hashtable-keys(3scm)hashtable-contains?(3scm)
β¨ 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
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2023 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms