[erlang-questions] lists:keyfind as an alternative to lists:keysearch

Bjorn Gustavsson bgustavsson@REDACTED
Wed Jan 21 16:49:24 CET 2009


On Wed, Jan 21, 2009 at 4:40 PM, mats cronqvist <masse@REDACTED> wrote:
> Bjorn Gustavsson <bgustavsson@REDACTED> writes:
>
>>>  seems i've written this function at least a thousand times;
>>>
>>> keyfind(Key,N,List) ->
>>>  {value,{Key,Val} = lists:keysearch(Key,N,List),
>>>  Val.
>>
>> This version only works for tuples of size 2.
>
>  it was meant as an example, not an implementation proposal. i didn't
>  balance the braces either :<

Yes, but if the tuple has more than two elements, how do you know
which field the caller is interested in? Do you assume that it always
the element after the key? What is the caller is interested in several
fields of the tuple?

/Bjorn
-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list