This one’s for you Vincent. (But feel free to chime in Ethan)
- It seems that the best way to elegantly and efficiently write a function to find a key in a hash table, dictionary, etc is to write a function with two return values. First value is whether or not the item was located, second value is the actual value for the key. Only problem I’m not sure that there are any cases where having a key in the hash table with the value of FALSE would be treated any differently then just not having the key in the table in the first place.
- If we accept that there are functions that must return multiple values to be useful or efficient, [e.g. HashLookup or yuv2rgb()], then as far as I can see, you have three options:
- Language support for real multiple values returned from functions
- user kludged multiple values using list or a data structure
- language kludge it by using output variables passed by reference or mutable data structure to the function.
I don’t like option 2 because it forces consing / memory allocation when it could potentially be avoided and option 3 requires mutable data structures or pass by reference, and those are not very Functional Programming friendly.