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

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

by Ellinor :: Rate this Message:

Reply to Author | View in Thread

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)