Table of contents
utility functions
system input
read an integer from system input
read_int() -> list_to_integer(string:strip(string:chomp(io:get_line("")))).
read a list of integers, one number per line
read_numbers() ->
Line = io:get_line(""),
case Line of
eof -> [];
Line -> [ list_to_integer(string:chomp(Line)) | read_numbers() ]
end.
read a list of floats, one number per line
read_floats() ->
Line = io:get_line(""),
case Line of
eof -> [];
Line -> [ list_to_float(string:chomp(Line)) | read_floats() ]
end.
read all lines from the system input in reverse order
read_in_reverse() ->
Line = io:get_line(""),
case Line of
eof -> [];
Line -> [ read_in_reverse() | string:chomp(Line) ++ "\n" ]
end.
system output / debugging
write the type of a variable
write_type(V) -> if
is_integer(V) -> io:fwrite("integer\n");
is_list(V) -> io:fwrite("list\n");
is_atom(V) -> io:fwrite("atom\n");
true -> io:fwrite("unknown!\n")
end.
write the value of a non-sting variable
write_val(V) ->
if
is_float(V) -> io:fwrite(float_to_list(V) ++ "\n");
is_integer(V) -> io:fwrite(integer_to_list(V) ++ "\n");
is_list(V) -> lists:foreach(fun write_val/1, V), io:fwrite("\n");
is_atom(V) -> io:fwrite(V), io:fwrite("~n");
is_tuple(V) -> write_val(tuple_to_list(V));
is_function(V) -> io:fwrite("#function\n");
true -> io:fwrite("????\n")
end.
write a string as a line
write_ln(S) -> io:fwrite(string:chomp(S) ++ "\n").
write a float
write_float(V) -> float_to_list(V) ++ "\n".
print a list of floats
write_floats(List) -> lists:foreach(fun print_float/1, List).
write an integer
write_integer(N) -> io:fwrite(integer_to_list(N) ++ "\n").
write a list of integers
write_integers(List) -> lists:foreach(fun write_integer/1, List).
other
strip all leading and trailing whitespace
strip_all(L) -> string:strip(string:chomp(L)).
call a function N times
repeat(N, F) -> [F() || X <- lists:seq(1, N) ].