"guarded" fetch from hashed array



On 02/08/2012 11:06 AM, Edd Barrett wrote:
> It seems like the 'hash[x] := error(...)' declaration is syntactic, in
> that it will only match for hashes called 'hash'; the behavior is not
> associated with the hash itself.
That statement defines a function named hash, with one input variable x. 
You are also
defining an array with the same function as the function and when use 
ask for hash[something]
in some cases Maxima will give you the value of the function and in 
other cases the
value of the array. That is a hack which should be handled with care.
Also, notice that hash[x] := defines a "memoizing function", which will 
store its value in
memory and then return it. For instance:

(%i4) hash[b];
ERROR  b
#0: lambda([x],error("ERROR ",x))(x=b)

The value lambda([x],error("ERROR ",x))(x=b) was stored in memory and 
then its result shown.
In other words, every time you try hash[key] with an undefined key, you 
are using up more
memory.

Regards,
Jaime