[erlang-questions] working with sets

Vance Shipley <>
Fri Jan 4 13:11:37 CET 2008


On Fri, Jan 04, 2008 at 12:44:35PM +0100, Richard Carlsson wrote:
}  If your elements are really integers within a limited range

They're string()'s in my case.

On Fri, Jan 04, 2008 at 12:42:04PM +0100, Ulf Wiger wrote:
}  Just understanding the man page for sofs is a bit of a
}  challenge, though.  (:

It is indeed.  It seems that if one were clever sofs would
provide a neat solution.  In the mean time ...

I implemented my sets of sets over ordsets fairly simply.
My only dissatisfaction is that I don't have a lazy way
to find the first set in the set of sets which contains
an element.  I'm all about lazy evaluation.

	-Vance

-module(mysofs).
-export([new/0, add_set/2]).


new() ->
	ordsets:new().

add_set(List, Set) when is_list(List) ->
	case is_element_in_sets(List, Set) of
		false ->
			Elem = ordsets:from_list(List),
			ordsets:add_element(Elem, Set);
		true ->
			exit(badarg)
	end.

is_element_in_sets([], _) ->
	false;
is_element_in_sets([H|T], Set) when is_list(H) ->
	F = fun(E, AccIn) ->
			case ordsets:is_element(H, E) of
			  	true -> 
					true;
			  	false -> 
					AccIn
			end
	end,
	case ordsets:fold(F, false, Set) of
		true ->
			true;
		false ->
			is_element_in_sets(T, Set)
	end;
is_element_in_sets(_, _) ->
	exit(badarg).




More information about the erlang-questions mailing list