Skip to content

Instantly share code, notes, and snippets.

@halfelf
Created September 4, 2012 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halfelf/3617940 to your computer and use it in GitHub Desktop.
Save halfelf/3617940 to your computer and use it in GitHub Desktop.
Find an element is in a tuple or not in Erlang.
-module(tuple_util).
-export([is_element_of_tuple/2]).
is_element_of_tuple(Tuple, Element) ->
lists:member(Element, tuple_to_list(Tuple)).
% or like this:
is_element_of_tuple(E, Tuple) ->
is_element_of_tuple(E, Tuple, 1, tuple_size(Tuple)).
is_element_of_tuple(E, T, I, S) when I =< S ->
case element(I, T) of
E -> true;
_ -> is_element_of_tuple(E, T, I+1, S)
end;
is_element_of_tuple(_, _, S, S) -> false. %Done all the elements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment