尝试在Erlang中编译时出现语法错误,嵌套if语句

guicsvcw  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(186)

I am currently working on this class assignment, which is to create a btree in Erlang and create a function able to delete an Element on it. From my perspective I can't understand the error Erlang is returning me when trying to compile my code.
btree.erl:211: syntax error before: 'end'
Which to my knowledge isn't true, but I must be wrong? I was suspecting that it lies in the nested if clause but I tried coding the nested if statement according to here Nested If Clause .
I suspect it might just be a tiny issue that I am too stressed/blind to see right now. Any help would be appreciated.

deleteBT(BTree = {ElementAtom,Height,Links,Rechts}, Element) ->
    if 
   
        Element > ElementAtom -> 
            NeuRechts = deleteBT(Rechts, Element),
            Hanoi = findHeight(NeuRechts, Links),
            {ElementAtom,Hanoi,Links,NeuRechts};
        
        Element < ElementAtom ->    
            NeuLinks = deleteBT(Links, Element),
            Hanoi = findHeight(NeuLinks, Rechts),
            {ElementAtom,Hanoi,NeuLinks,Rechts};

        Element == ElementAtom -> 
            if 
                BTree == {ElementAtom, Height, Links,{}} -> Links;
                
                BTree == {ElementAtom, Height, {},Rechts} -> Rechts;
                
                BTree == {ElementAtom, Height, {},{}} -> {};
                
                BTree == {ElementAtom, Height, Links,Rechts} ->
                    Kleinster = kLZahl(Rechts), %Findet uns die Kleinste Zahl vom übergebenen Baum
                    RechtsNeu = deleteBT(Rechts, Kleinster), 
                    Hanoi = findHeight(RechtsNeu, Links),
                    {Kleinster, Hanoi, Links, RechtsNeu};
                true -> -1
                end;
        true -> -1;
        end.
ua4mk5z4

ua4mk5z41#

我在你的评论中看到你已经解决了这个问题。
尽管如此,我还是想借此机会向您推荐一种简单的方法,使用模式匹配和较少的if来改进代码:

delete_bt({ElementAtom, _, Links, Rechts}, Element) when Element > ElementAtom ->
    NeuRechts = delete_bt(Rechts, Element),
    Hanoi = find_height(NeuRechts, Links),
    {ElementAtom, Hanoi, Links, NeuRechts};
delete_bt({ElementAtom, _, Links, Rechts}, Element) when Element < ElementAtom ->
    NeuLinks = delete_bt(Links, Element),
    Hanoi = find_height(NeuLinks, Rechts),
    {ElementAtom, Hanoi, NeuLinks, Rechts};
delete_bt({ElementAtom, _, Links, {}}, ElementAtom) ->
    Links;
delete_bt({ElementAtom, _, {}, Rechts}, ElementAtom) ->
    Rechts;
delete_bt({ElementAtom, Height, Height, Links, Rechts}, ElementAtom) ->
    Kleinster = kLZahl(Rechts), %Findet uns die Kleinste Zahl vom übergebenen Baum
    RechtsNeu = delete_bt(Rechts, Kleinster),
    Hanoi = find_height(RechtsNeu, Links),
    {Kleinster, Hanoi, Links, RechtsNeu};
delete_bt(_, _) ->
    -1.

相关问题