Erlang Programs (L20) Flashcards

1
Q

Are there void functions in Erlang?

A

No, all functions return a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

For multi-expression function bodies, which expression is returned by the function?

A

The last expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is wrong the following syntax?

func_name [parm1 parm2 parm3]:
expression_1,
expression_2,
expression_3;

A

func_name (parm1, parm2, parm3) ->
expression_1,
expression_2,
expression_3.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Invoke the following function:
hello_world () ->
io:fwrite(“Hello World\n”).

A

hello_world().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which format specifier is used to print values in natural form (such as ints, floats, etc.) when using io:fwrite ?

A

~w

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which format specifier is used to print strings when using io:fwrite ?

A

~s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens when a string is printed using the ~w format specifier?

A

The string’s ASCII values are printed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In what two ways can newline characters be printed using io:fwrite ?

A

\n or ~n.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Is the following valid?
io:fwrite(“~w, ~w, ~s~n”, 3, 4.5, “cat”).

A

False: io:fwrite(“~w, ~w, ~s~n”, [3, 4.5, “cat”]).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Multi arity functions are defined within the same function definition in Erlang.

A

False, each definition is terminated by a period - these are completely separate definitions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can different arities of the same function call one another in Erlang?

A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can we have multiple variations of a
function, all with the same arity in Erlang?

A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When multiple versions of a
function exists, all with the same arity, how does Erlang decide which to use?

A

Using pattern matching.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Are multiple variations of a function, all with the same arity, defined separately or within the same Erlang statement?

A

Within the same statement, as different variations are separated by a semi colon and only the last definition ends in a period.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Define an anonymous function (in Erlang) which returns its single parameter multiplied by 2.

A

fun (X) -> X * 2 end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Erlang also has a map, reduce, and filter function defined in the lists module, which behave the same way as they do in Clojure.

A

False - Erlang’s reduce function is called fold.

17
Q

What is the Erlang equivalent of Clojure’s doseq function?

A

lists:foreach

18
Q

Which attribute is used to define an Erlang module? Where is it placed?

A

-module(mod_name).
Placed atop the module source code file.

19
Q

What is the default code path in Erlang?

A

The current working directory and Erlang’s system library location.

20
Q

In what environment variable should directories be defined in order to add them to the code path? (Erlang)

A

ERL_LIBS

21
Q

Functions must be imported from another Erlang module (using -import(…)) in order to use them.

A

False - importing is optional, but functions must be exported to be callable.

22
Q

The following successfully exports 3 arities of the baz function.
-export(baz/1, baz/2, baz/3).

A

False, they must be placed in a list:
-export( [baz/1, baz/2, baz/3] ).

23
Q

When is an Erlang function considered private?

A

When it is not specified in the export list.

24
Q

Where is the -export() attribute placed?

A

At the top of the source code file.

25
Q

What does the -import() attribute do?

A

The import attribute eliminates the need to fully qualify the function with the module name.

26
Q

Use the import attribute to validate non fully qualified use of the append (arity 1) and map (arity 2) functions from the lists module.

A

-import(lists, [append/1, map/2]).

27
Q

Can multiple files be compiled at once using erlc?

A

Yes.

28
Q

What command is used to execute compiled Erlang code?

A

erl

29
Q

What erl execute option prevents dropping into the interactive Eshell?

A

-noshell

30
Q

What commands indicate a module and function combination to load an run?

A

-s or -run

31
Q

Can only a module be specified following the -s/-run options?

A

Yes, in which case the start() function is assumed.

32
Q

What is the primary difference between -s and -run?

A

If command line arguments are provided, s will pass them as atoms (constants), while run will pass them as strings.

33
Q

A second -s option is typically passed with what arguments?

A

-s init stop

34
Q

What does the second “-s init stop” do?

A

Tells erl to shutdown the Erlang environment once the program is finished.