为什么在Erlang中显示badarg?

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

这是我的Erlang代码:

-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 (H == "planets ") -> 
deal([], 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.

这是主代码(另一个将运行我的代码的文件):

#!/usr/bin/env escript
%-module(main).
%-export([main/1, print_list/1, usage/0]).
%% -*- erlang -*-
%%! -smp enable -sname factorial -mnesia debug verbose
main([String]) ->
    try
        CSV = csv:parse_file(String),
        F =solarSystem:process_csv(CSV),
        print_list(F)
    catch
        A:B -> io:format("~w : ~w~n",[A,B]),
        usage()
    end;
main(_) ->
    usage().

print_list([])-> [];
print_list([H|T])->io:format("~s~n",[H]),print_list(T).

usage() ->
    io:format("usage: escript main.erl <filename>\n"),
    halt(1).

这个错误是因为重新开启VSCode而造成的,但是昨天我开启先前的VSCode时,这个错误并没有出现。我可以在关闭VSCode之前执行这个错误,而不需要编译主函式。这个影像显示了这个错误。

6qqygrtg

6qqygrtg1#

您使用trycatch隐藏了错误。删除它,Erlang会告诉您错误的确切来源。如果您不想这样做,请使用catch A:B:SS是堆栈跟踪;打印出来。

相关问题