[erlang-questions] 2 questions: Guards & Variables

Rick Pettit rpettit@REDACTED
Wed Nov 15 22:08:29 CET 2006


On Wed, Nov 15, 2006 at 01:32:15PM +0100, Nils Müllner wrote:
> hi,
> i currently implement depth first search tree for distributed 
> fault-tolerance measurement. the enclosed module is just one part.
> 
> to run the attached file
>   1. enter sample matrix:
>       Matrix = [{a,0.1,pid,[{b,0.1},{c,0.1},{d,0.1}], cur, 
> stab},{b,0.1,pid,[{e,0.1},{f,0.1}], cur, stab},{c,0.1,pid,[{g,0.1}], 
> cur, stab},{d,0.1,pid,[{h,0.1},{i,0.1}], cur,    stab},{e,0.1,pid,[], 
> cur, stab},{f,0.1,pid,[], cur, stab},{g,0.1,pid,[], cur, 
> stab},{h,0.1,pid,[], cur, stab},{i,0.1,pid,[], cur, stab}].
>   2 compile
>       c(dfs_init_matrix).
>   3. run
>       dfs_init_matrix:dfs_init_matrix(Matrix).
> 
> questions:
> 1. in line 81 (in function rip_neighbours the line Bool = 
> lists:member(First, Raw_Neighbours),) i have to assign the desired value 
> to variable Bool. if i used it directly in the if     following clause i 
> get a guard error. why do i have to use this work-araound and why is it 
> not possible to directly use
>       if
>           lists:member(First, Raw_Neighbours) ->
>                  ...
>   as guard?

Functions could introduce side-effects (which are disallowed in erlang guards).

> 2. in the end, everything is correctly assigned into variable 
> Return_Value in line 50. but when i return this value, everything gets 
> corrupted... why?

No time to answer this atm--sorry.

-Rick

> further information:
> *the tree built can be seen in http://www.mue-tech.com/DFS.png
> *the desired result is written to variable Return_Value and printed 
> before crash
> *the matrix has the following structure
>   [{Node_Name,Node_Fault_Possibility,Node_PID,[{Neighbours_Name, 
> Link_Fault_Possibility}, 
> more_neighbours],Current_Value,Stable_Value},more_nodes]
> *the function gets a matrix where all current values are arbitrary. to 
> run the simulator (super module), this module has to assign the stable 
> path (Stable_Value) for each node and finally add the list, in which the 
> nodes will be visited by dfs-algorithm (compare: 
> http://en.wikipedia.org/wiki/Depth-first_search )
> 
> thanks in advance for any help...
> 
> kind regards
> nils müllner

> -module(dfs_init_matrix).
> -export([dfs_init_matrix/1]).
> dfs_init_matrix(Matrix)->
>     My_Elem = get_elem(a,Matrix),
> 	{Name, Node_Fault, PID, Neighbours, Current, _} = My_Elem,
> 	New_My_Elem = {Name, Node_Fault, PID, Neighbours, Current, [root]},
>     Rest_Matrix = lists:delete(My_Elem, Matrix),
>     Stable_Matrix = [New_My_Elem],
> 	Path = [root],
>     Iterative_List = [root],
>     My_Children = get_children(Iterative_List, Matrix, a),
>     [Next_Child|Other_Children] = My_Children,
>     Receive = do_init(Next_Child, Path, Stable_Matrix, Rest_Matrix, Matrix, Iterative_List, Other_Children),
>     Receive.
> 
> do_init(My_Name, Path, Stable_Matrix, Rest_Matrix, Matrix, It_List, My_Brothers) ->
>     My_Elem = get_elem(My_Name, Matrix),
>     {Name, NFP, PID, Nei, Cur, _} = My_Elem,
>     New_Path = lists:append(Path,[My_Name]),
>     Stable_My_Elem = {Name, NFP, PID, Nei, Cur, New_Path},
>     New_Stable_Matrix = lists:append(Stable_Matrix, [Stable_My_Elem]),
>     New_Rest_Matrix = lists:delete(My_Elem, Rest_Matrix),
>     New_It_List = lists:append(It_List,[My_Name]),
>     My_Children = get_children(New_It_List, Matrix, My_Name),
> 	if 
>         length(New_Rest_Matrix) > 0 ->
> 			if
> 				length(My_Children) > 0 ->
> 		            [Next_Child|Other_Children] = My_Children,
> 					{New_New_Stable_Matrix, New_New_Rest_Matrix, New_New_It_List} = do_init(Next_Child, New_Path, New_Stable_Matrix, New_Rest_Matrix, Matrix, New_It_List, Other_Children);
> 				true ->
> 					New_New_Stable_Matrix = New_Stable_Matrix,
>                     New_New_Rest_Matrix = New_Rest_Matrix,
>                     New_New_It_List = New_It_List
> 			end,
> 			if
> 				length(My_Brothers) > 0 ->
> 		            [Next_Brother|Other_Brothers] = My_Brothers,
> 					{New_New_New_Stable_Matrix, New_New_New_Rest_Matrix, New_New_New_It_List} = do_init(Next_Brother, Path, New_New_Stable_Matrix, New_New_Rest_Matrix, Matrix, New_New_It_List, Other_Brothers);
> 				true ->
> 					New_New_New_Stable_Matrix = New_New_Stable_Matrix,
> 					New_New_New_Rest_Matrix = New_New_Rest_Matrix,
> 					New_New_New_It_List = New_New_It_List
> 			end,
> 			{New_New_New_Stable_Matrix,New_New_New_Rest_Matrix,New_New_New_It_List};
> 		true ->
> 			New_New_Stable_Matrix = lists:keysort(1,New_Stable_Matrix),
> 			Return_Value = {New_It_List, New_New_Stable_Matrix},
> 			io:format("READ THIS!!!~p~n",[Return_Value]),
> 			Return_Value
> 	end.
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> get_children(Iterative_List, Matrix, Name)->
>     Elem = get_elem(Name,Matrix),
>     {_, _, _, Neighbours, _, _} = Elem,
>     Raw_Neighbours = raw_neighbours(Neighbours,[]),
>     rip_neighbours(Raw_Neighbours,Iterative_List).
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> get_elem(Elem, Client_List)->
>     [First|Rest] = Client_List,
>     {Name, _,_,_,_,_} = First,
>     if
>     	Name == Elem ->
>             First;
>         true ->
>             get_elem(Elem, Rest)
>     end.
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> raw_neighbours([],Neighbours)->
>     Neighbours;
> raw_neighbours(Full_List,Neighbours)->
>     [First|Rest] = Full_List,
>     {Name,_} = First,
>     New_Neighbours = lists:append(Neighbours,[Name]),
>     raw_neighbours(Rest,New_Neighbours).
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> rip_neighbours(Raw_Neighbours,[])->
>     Raw_Neighbours;
> rip_neighbours(Raw_Neighbours,Iterative_List)->
>     [First|Rest] = Iterative_List,
>     Bool = lists:member(First, Raw_Neighbours),
>     if 
>     	First == root ->
>             New_Raw_Neighbours = Raw_Neighbours;
> 		true ->
> 			if
>             	Bool ->
>                     New_Raw_Neighbours = lists:delete(First, Raw_Neighbours);
>                 true ->
>                     New_Raw_Neighbours = Raw_Neighbours
> 			end
> 	end,
> 	rip_neighbours(New_Raw_Neighbours,Rest).
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list