[racket] question about implementation of hash-ref!

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Fri Nov 7 16:12:44 EST 2014

The simplest, boring, answer is that short-circuiting requires that
hash-ref! be a macro, which makes it difficult to use in first-class
ways, like storing it inside of a list. There are many other functions
like this and the pattern of accepting a value OR a thunk is pretty
common, for this reason.

Jay

On Fri, Nov 7, 2014 at 4:08 PM, Matthew Butterick <mb at mbtype.com> wrote:
> 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.
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users



-- 
Jay McCarthy
http://jeapostrophe.github.io

           "Wherefore, be not weary in well-doing,
      for ye are laying the foundation of a great work.
And out of small things proceedeth that which is great."
                          - D&C 64:33


Posted on the users mailing list.