Erlang Programs (L20) Flashcards
Are there void functions in Erlang?
No, all functions return a value.
For multi-expression function bodies, which expression is returned by the function?
The last expression.
What is wrong the following syntax?
func_name [parm1 parm2 parm3]:
expression_1,
expression_2,
expression_3;
func_name (parm1, parm2, parm3) ->
expression_1,
expression_2,
expression_3.
Invoke the following function:
hello_world () ->
io:fwrite(“Hello World\n”).
hello_world().
Which format specifier is used to print values in natural form (such as ints, floats, etc.) when using io:fwrite ?
~w
Which format specifier is used to print strings when using io:fwrite ?
~s
What happens when a string is printed using the ~w format specifier?
The string’s ASCII values are printed.
In what two ways can newline characters be printed using io:fwrite ?
\n or ~n.
Is the following valid?
io:fwrite(“~w, ~w, ~s~n”, 3, 4.5, “cat”).
False: io:fwrite(“~w, ~w, ~s~n”, [3, 4.5, “cat”]).
Multi arity functions are defined within the same function definition in Erlang.
False, each definition is terminated by a period - these are completely separate definitions.
Can different arities of the same function call one another in Erlang?
Yes.
Can we have multiple variations of a
function, all with the same arity in Erlang?
Yes.
When multiple versions of a
function exists, all with the same arity, how does Erlang decide which to use?
Using pattern matching.
Are multiple variations of a function, all with the same arity, defined separately or within the same Erlang statement?
Within the same statement, as different variations are separated by a semi colon and only the last definition ends in a period.
Define an anonymous function (in Erlang) which returns its single parameter multiplied by 2.
fun (X) -> X * 2 end