and-ing, or-ing?
Vladimir Sekissov
svg@REDACTED
Sun May 18 21:06:03 CEST 2003
Good day,
existsVertex(N, Graph) and existsVertex(M, Graph)
or better
existsVertex(N, Graph) andalso existsVertex(M, Graph)
andalso - C-like "and", second argument is not calculated if first is
false.
Or-ing:
existsVertex(N, Graph) or existsVertex(M, Graph)
existsVertex(N, Graph) orelse existsVertex(M, Graph)
And slightly shorter function for your case would be
existsVertexes(Vertexes, {graph, G}) ->
ExistsVertexes = [N || {N, _} <- G, lists:memeber(N,Vertexes) == true],
length(Vertexes) == length(ExistsVertexes).
existsVertexes([N, M], Graph).
Best Regards,
Vladimir Sekissov
chris.danx> How do you "and" two truth states? In the following "addEdge" function
chris.danx> It is supposed to add an edge if and only if both vertices exist... the
chris.danx> function existsVertex is defined to return true if a vertex exists in a
chris.danx> given graph.
chris.danx>
chris.danx>
chris.danx> %% return true if given vertex is in the graph.
chris.danx> %%
chris.danx> existsVertex(N, {graph, []}) ->
chris.danx> false;
chris.danx> existsVertex(N, {graph, [{N,Y}|Xs]}) ->
chris.danx> true;
chris.danx> existsVertex(N, {graph, [_|Xs]}) ->
chris.danx> existsVertex (N, {graph, Xs}).
chris.danx>
chris.danx>
chris.danx> %% create connection between given vertices
chris.danx> %%
chris.danx> addEdge (N, M, {graph, []}) ->
chris.danx> nil;
chris.danx> addEdge (N, M, {graph, L}) ->
chris.danx> bothExist = ???
chris.danx> if bothExist ->
chris.danx> {graph, []};
chris.danx> true ->
chris.danx> {error, {graph, L}}
chris.danx> end.
chris.danx>
chris.danx>
chris.danx> It's not complete yet, the return values are just temporary until I
chris.danx> figure out how to check the vertices exist...
chris.danx>
chris.danx>
chris.danx> Cheers,
chris.danx> Chris
chris.danx>
chris.danx> p.s. I might add a function which returns an existing {vertex, [edge]}
chris.danx> tuple to make it more efficient, but for now I'm keeping it simple.
More information about the erlang-questions
mailing list