[racket] question about implementation of hash-ref!

From: Matthew Butterick (mb at mbtype.com)
Date: Fri Nov 7 16:08:43 EST 2014

Since `hash-ref!` includes `or`-like functionality, is there a reason it's not implemented with short-circuit logic similar to `or`?

In other words, the function `hash-ref!` is invoked thus:

(hash-ref! hashtable key new-value-if-needed)

What I notice is that if you put an expression in the new-value position, like so:

(hash-ref! hashtable key (expression-that-produces-value))

Then this expression will always be evaluated before the function is called.

But this seems wasteful, since if 'key' exists in 'hashtable', the expression won't be needed.

`hash-ref!` will also take a thunk, so you can manually delay evaluation of the value expression by wrapping it in λ:

(hash-ref! hashtable key (λ () (expression-that-produces-value)))

But I'm curious why delayed evaluation isn't the default behavior.

Posted on the users mailing list.