[erlang-questions] Noob questions: Searching a tree

Hynek Vychodil <>
Tue Dec 9 13:07:43 CET 2008


Look at http://www.erlang.org/doc/reference_manual/expressions.html#guards

comma works as andalso - lazy and
semicolon works as orelse - lazy or

On Tue, Dec 9, 2008 at 12:25 PM, Edward Stow <> wrote:

> 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
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



-- 
--Hynek (Pichi) Vychodil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081209/18262f26/attachment.html>


More information about the erlang-questions mailing list