[erlang-questions] Noob questions: Searching a tree

Edward Stow ed.stow@REDACTED
Tue Dec 9 12:25:38 CET 2008


Is this a friendly place to ask very simple questions about Erlang and
functional programming.  I'm learning both at the same time.  For
example I want to search for a value in the nodes of a tree structure.
 The tree is simply nested lists.  For example

6> tut1:search(a, [b, [a]]).
7> tut1:search(a, [b, [c]]).
8> tut1:search(a, [ [a], b]).

My implementation is: Is this implementation OK or how else would it
be performed.


%% Search for an element in tree (nested lists)

search(_, []) ->

search(A, [A|_]) ->

search(A, [H|T]) when list(H), length(H) > 0 ->
    [H1|T1] = H,
    search(A, [H1 | [T1 | T]]);

search (A, [_|T]) ->
    search(A, T).

Question:  I'm not really sure of the semantics of the comma operator
in the function when clause:
search(A, [H|T]) when list(H), length(H) > 0 ->
Can somebody point me towards some relevant documentation.


Edward Stow

More information about the erlang-questions mailing list