mardi 1 décembre 2020

Does erlang supports function inside an if clause? [duplicate]

I was trying to do a little fix to my already worked solution on a coursework on erlang. In line 22 I used an condition to direct compare the string, which I knew today that was a bit inaccurate, so I made a new function which return me a flag to let me do special cases or sth. But it actually throws me error:

solarSystem.erl:22: call to local/imported function check3/3 is illegal in guard
solarSystem.erl:80: function check3/3 already defined
solarSystem.erl:79: Warning: function check3/3 is unused
solarSystem.erl:80: Warning: function check3/3 is unused

which really confuses me since I thought it won't be a problem in any other language. Here's my program:

-module(solarSystem).
-export([process_csv/1,is_numeric/1]).

is_numeric(L) ->
    S = trim(L,""),
    Float = (catch erlang:list_to_float(  S)),
    Int   = (catch erlang:list_to_integer(S)),
    is_number(Float) orelse is_number(Int).

trim([],A)->A;
trim([32|T],A)->trim(T,A);
trim([H|T],A)->trim(T,A++[H]).

process_csv([_H|T]) -> process_line(T, "").

process_line([], A) -> [A];
process_line([H|T], A) ->
    process_line(T, A ++ deal(H, "", 1)).

deal([], B, _Count) -> B ++ "\n";
deal([H|T], B, Count) ->
    if (check3([H|T], 1, 0) == 1) ->
        %%where the error happened. This is line 23
        deal([], B ++ planetcheck([H|T], "", 1), Count);
    true ->
        case is_numeric(H) of
            true ->  if Count == 6 ->
                        deal([], B ++ subcheck([H|T], "", 6) ++ "];", Count+1);
                    true ->
                        deal(T, B ++ H ++ ",", Count+1)
                    end;
            false -> if Count == 2 ->
                        deal(T, B ++ "=[\"" ++ H ++ "\",", Count+1);
                    true ->
                        deal(T, B ++ H, Count+1)
                    end
        end
    end.

subcheck([], C, _Scount) -> C;
subcheck([H|T], C, Scount) ->
    case is_numeric(H) of
        true -> if (Scount == 6) and (T == []) ->
                    subcheck(T, C ++ H ++ ",[]", Scount+1);
                true ->
                    subcheck([], C ++ H ++ "," ++ noone(T, C), Scount+1)
                end;
        false -> if T == [] ->
                    subcheck(T, C ++ H ++ "]", Scount+1);
                 true -> 
                    subcheck(T, C ++ H ++ ",", Scount+1)
                 end
    end.

noone([], D) -> D;
noone([H|T], D) ->
    case is_numeric(H) of
        true ->
            noone([], D ++ T);
        false ->
            if T == [] ->
                subcheck(T, D ++ "["++ H ++ "]", 7);
            true ->
                subcheck(T, D ++ "["++ H ++ ",", 7)
            end
        end.

planetcheck([], E, _Pcount) -> E ++ "];";
planetcheck([H|T], E, Pcount) ->
    if Pcount == 1 ->
        planetcheck(T, E ++ H ++ "=[", Pcount+1);
    true ->
        if T == "" ->
            planetcheck(T, E ++ H, Pcount+1);
        true -> 
            planetcheck(T, E ++ H ++ ",", Pcount+1)
        end
    end.

check3([],_Ccount, Flag) -> Flag. 
check3([H|T], Ccount, _Flag) ->
    case is_numeric(H) of
    true -> 
            check3([T], Ccount+1, 0);
    false -> if (Ccount == 3) and (T == []) ->
                check3([T], Ccount+1,1);
             true ->
                check3([T], Ccount+1,0)
            end
    end.

NB: The task was taking in a huge list and do some pattern changing like add "" or [], but I'm already done with that part.

Aucun commentaire:

Enregistrer un commentaire