« Return to Thread: This code doesn't work, can't seem to find the reasons why. Help me to see them? (erlang)

Re: This code doesn't work, can't seem to find the reasons why. Help me to see them? (erlang)

by different :: Rate this Message:

Reply to Author | View in Thread

hi,
the last case branch ends with an ";"
there is a ioo typo
there is on more ")" than "(" in the test function
the first isEmpty function ends with an . instead with an ";"

Regards,
 
Ellinor wrote:
Hey!

Here's a "program" I wrote but I can't seem to get it to work. I have no idea where it has gone wrong... My bf "borrowed" the code to make it better but now I just can't see what's wrong with it. I'm a noob at Erlang so yeah. And I can't ask my boyfriend either cause he's off traveling.

So... whatcha think?

-module(tree).
-export([new/0, newNode/1, isEmpty/1, insert/2, print/1, test/0]).

new() ->
    empty.

newNode(e)->
    {E, new(), new()}.

isEmpty(empty) ->
    true.
isEmpty(_) ->
    false.

insert(E, Tree) ->
    case Tree of empty ->
            newNode(E);
        {E, _, _} ->
            Tree;
        {X, L, R} when X > E  ->
            {X, insert(E, L), R};
        {X, L, R} ->
            {X, L, insert(E, R)};
        end.

print(Tree) ->
    print2(Tree),
    io:nl().

print2(empty) ->
    ok;
print2({E, L, R}) ->
    print2(L),
    ioo:format("~w ", [E]),
    print2(R).

test() ->
    X=insert(6,
           insert(3,
                insert(11,
                       insert(15,
                              insert(25,
                                     insert(2,
                                            insert(10, new())))))))),
print(X).

 « Return to Thread: This code doesn't work, can't seem to find the reasons why. Help me to see them? (erlang)