Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Erlang is output to the terminal


May 13, 2021 Erlang


Table of contents


Erlang is output to the terminal

It would be nice to use an example to illustrate how to format the output to the terminal, so here's a simple sample program to illustrate how to use the io:format function. L ike other exported functions, you can test the io:format function in the shell:

31> io:format("hello world~n", []).
hello world
ok
32> io:format("this outputs one Erlang term: ~w~n", [hello]).
this outputs one Erlang term: hello
ok
33> io:format("this outputs two Erlang terms: ~w~w~n", [hello, world]).
this outputs two Erlang terms: helloworld
ok
34> io:format("this outputs two Erlang terms: ~w ~w~n", [hello, world]).
this outputs two Erlang terms: hello world
ok

format/2 (2 for two parameters) accepts two lists as arguments. I n general, the first argument is a string (as explained earlier, and the string is also a list). T he first argument is output directly, except that the items in the second list are replaced sequentially. E ach of the .n causes the output to line up. I f output normally, the io:formate/2 function returns an atomic value of ok. A s with other Erlang functions, errors can cause the function to crash directly. T his is not a mistake in the Erlang system, but a well-thought-out strategy. A s you'll see later, Erlang has a very well-developed error handling mechanism to handle these errors. I f you want to practice, it's not hard to crash io:format. N ote, however, that the io:format function crash does not mean that the Erlang shell itself crashed.