Ulf Wiger (AL/EAB) <>
Tue Nov 9 11:17:52 CET 2004

This is consistent with e.g. lists:keysearch/3.
The functions operating on tuple lists do not enforce
any notion that the list cannot contain anything but tuples.

If you want such a function, then

keymember(Key,Pos,List) ->
      fun(T,Bool) when is_tuple(T) ->
	      Bool or (element(Pos,T) == Key);
	 (_,_) ->
      end, false, List).

would do the trick, but note that it always has to traverse
the entire list. If it would bail out as soon as it came 
across the key it's looking for, it would not consistently
fail for lists that are not of type [tuple()] -- only for 
those that have a non-tuple before an object that satisfies
the search, or for lists that have non-tuples and no tuple T
where element(Pos, Tuple) == Key. Example:

keymember(2,1,[{1,a},{2,b}]) -> true
keymember(2,1,[{1,a},{3,c}]) -> false
keymember(2,1,[{1,a},{3,c},foo]) -> exit(badarg)
keymember(2,1,[{1,a},{2,b},foo]) -> true
keymember(2,1,[{1,a},foo,{2,b}]) -> exit(badarg)


> -----Original Message-----
> From: 
> [mailto:]On Behalf Of Samuel Rivas
> Sent: den 9 november 2004 09:55
> To: 
> Subject: lists:keymember
>   Shouldn't this fail with a badarg? The argument does not 
> complain with
> the description of the function ("keymember(Key, N, Tuplelist) [...]
> Tuplelist = [tuple()]")
> 1>  lists:keymember(foo, 1, [foo]).
> false
> -- 
> 	Samuel

More information about the erlang-questions mailing list