[erlang-questions] Noob questions: Searching a tree

Edward Stow <>
Tue Dec 9 12:25:38 CET 2008


Hi

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]]).
true
7> tut1:search(a, [b, [c]]).
false
8> tut1:search(a, [ [a], b]).
true

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

-module(tut1).
-export([search/2]).

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

search(_, []) ->
    false;

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

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.

Thanks
-- 

Edward Stow



More information about the erlang-questions mailing list