[racket] lambda as default value to dict-ref

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri Feb 15 22:17:27 EST 2013

If the third argument to `dict-ref' (and `hash-ref') is a function,
then the function is called as a failure thunk. If the third argument
is any other kind of value, then the value is returned as the result in
case of failure.

This is convenient but, yes, potentially confusing.

If you want a default result value that is a procedure, then you have
to use the failure-thunk mode to return the procedure:

 ((dict-ref A key (lambda () (lambda (_) "wrong key"))) some-params)

At Fri, 15 Feb 2013 21:54:58 -0500, J G Cho wrote:
> So, I have a dict A that looks like (list ("string as key" . (lambda
> (arg) ...) ...)
> 
> I do ((dict-ref A key (lambda (_) "wrong key")) some-params).
> 
> When keys are valid the above work as expected. When the key does not
> find any, I was expecting
> 
> ((lambda (_) "wrong key") some-params) but it fails with this msg:
> 
>  arity mismatch;
>  the expected number of arguments does not match the given number
>   expected: 1
>   given: 0
> 
> Upon further inspection,
> 
> (dict-ref A key (lambda (_) "wrong key"))  does not seem to return the
> lambda but seems to invoke it. Why so? What am I failing to understand
> here?
> 
> 
> Confused,
> 
> jGc
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

Posted on the users mailing list.