-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchapter2.txt
358 lines (214 loc) · 9.27 KB
/
chapter2.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
Chapter 2: modules
----
True or False: A module’s file name must have the same name as the module it holds.
True,
When you create a new module you must name the file module_name.erl
----
Is it true that a file named little_erlanger.erl would have the module definition
-module(little_erlanger).
True,
The module name must match the file name.
----
True or false: The first line of code in a module should be the module definition.
True,
The first line of code in a new module is the -module(). definition.
----
True or false: After the module definition you should export all public functions.
True,
following your module definition you should export public functions.
----
True or false: It's possible to export all modules in a function using -compile(export_all).
True,
If you would like to export all functions in your module you can use -compile(export_all). This is normally not
suggested to use in production but can be useful while starting to build your module.
----
Will the following definition export a single function?
-export([double/1]).
True,
Using -export([double/1]) will export a single function.
----
What is the function being exported?
-export([double/1]).
double/1,
The name of the function being exported is double and the /1 means that it takes 1 argument. This is called the arity of the function and is very important because you can name multiple functions double but give them all a different arity. We will discuss more about arity a little later on.
----
True or false: -export() takes a list as an argument
True,
-export() takes a list containing the functions that you would like to make public.
----
True or false: -export([double/1, triple/1]). will export two functions.
True,
-export([double/1, triple/1]). contains a list of two functions; double/1 and triple/1 that will be available for use outside of the function
----
True or false:
6 = double(3).
True,
double() is a function that will multiply the argument by 2. It may look like we are assigning the value of double(3) to the number 6, but we are not. Remember that in chapter 1 we learned that the = operator serves two purposes in Erlang. 1) It will assign a value to an unbound variable. 2) It will act as a pattern matching operator if the value of the left hand term is a bound variable or number. In this case the left hand term is the number 6 so a pattern match between it and the value returned by double(3) is performed, and the result is true.
----
How would you write the function double/1
You were not expected to be able to answer that yet. Let's look at how to do this now.
----
-module(little_erlanger).
-export([double/1]).
double(X) ->
X * 2.
What would the file name be for this module?
little_erlanger.erl
because the module is named little_erlanger
----
What will the <code>-export([double/1]).</code> expression do?
It will export the function double/1 making it available for use outside of the module.
----
What is the arity of double/1
1,
The arity of a function is the number of arguments that it takes. double/1 has an arity of 1, which means that the definition of the function will only take 1 argument.
----
What is the head of the function
double(X) ->
X * 2.
double(X),
the head of the function is the function name and a clause used for pattern matching. In the case of double(X) the clause is X which is a variable that could match against any single argument passed to the function. We will see how these clauses are more effectively used later on.
----
What is the body of
double(X) ->
X * 2.
X * 2,
The body of the function is the code that follows the ->
----
True or false: double(X) -> X * 2. will return the value of X * 2.
True,
The function will return the last result so there is no need to explicitly return any value.
----
True or false: in the previous example X is a variable.
True,
X represents the argument passed into the function.
----
True or false: in the previous example * is a math operator
True,
* is the multiplication operator and will multiply the variable X by the number 2.
----
What would be the steps for making an add() function in our module?
Put the function name and arity in the -export() definition
Put the function definition below* the -export() definition.
----
Is it true that our function name and arity would be
add/2.
True,
we want to take two numbers and add them together returning the answer to the caller.
----
True or false: our new -export() definition will look like this
-export([double/1, add/2]).
True,
our -export() definition now holds a list of two function names with their arity.
----
This is the function add/2
add(X, Y) ->
X + Y.
----
How many arguments does add/2 take?
Two,
it takes a X and a Y argument.
----
What does add/2 do with those arguments?
The function takes the X and Y argument and adds them together using the + operator.
----
What is the value returned to the caller?
The value returned will be the result of adding the X and Y variables together.
----
True or false: We can write functions without using a module.
True,
we can write functions as anonymous functions.
----
True or false: We can assign an anonymous function to a variable.
True,
we can assign an anonymous function to a variable in the same way that we assign other values to variables.
----
What is the variable Foo bound to in the following statement?
Foo = fun(X, Y) -> X + Y end.
Foo is bound to an anonymous function that takes two arguments and adds them together.
----
True or false: We can call the anonymous function contained in Foo with the command
Foo(2,3).
True,
Foo now has an arity of 2 and can be called like a regular function with arguments.
----
True or false: We can create a single anonymous function that does what both the add/2 and the double/1 functions did.
True,
we can create an anonymous function that does pattern matching on the arguments and determines which function it’s supposed to act as.
----
This is the anonymous function that acts as both* add/2 and double/1
MultiMath = fun({double, X}) -> {double, X * 2};
({add, X, Y}) -> {add, X + Y}
end.
it’s not best practice to do this with anonymous functions, this is simply to demonstrate how powerful they can be.
----
How can we have one function definition with multiple arity?
We can’t,
I cheated a little and replaced the normal arguments with an arity of /1 by using a tuple, notice the curly braces, to pass in multiple values to the function.
----
True or false: The semicolon separates the possible patterns that could be matched for the function?
True,
the semicolon marks the end of each possible match. Since we only have two possible matches we only have to use a semicolon at the end of the first possible match. The second possible match has the regular end. statement.
----
What is the first possible match for MultiMath?
{double, X},
if a tuple beginning with the atom double, followed by a single argument is passed to MultiMath it will match the first clause and allow the body of that clause execute.
----
What is the second possible match for MultiMath?
{add, X, Y},
if a tuple beginning with the atom add, followed by two arguments separated by commas is passed to MultiMath it will first check against the first clause, since it doesn’t match the first clause it will try against the second clause and match, causing the body of the second clause to execute.
----
True or false: We can call the anonymous function with the statement
MultiMath(3, 4).
False,
the arity of MultiMath is /1 so passing two arguments will result in the following error.
** exception error: interpreted function with arity 1 called with two arguments
----
True or false: We can call the anonymous function with the statement
MultiMath({add, 3, 4}).
True,
here we include a tuple with the atom add followed by the numbers 3 and 4.
----
Is it true that our returned value will also be a tuple?
True,
it becomes standard practice later on to primarily return a tuple from functions that have a leading atom. This is normally an atom that let’s you know if the function performed correctly and will return a tuple that looks like {ok, Value} if a function was able to process the arguments successfully.
----
What is the returned value from the statement
MultiMath({double, 7}).
{double, 14}.
----
What is the returned value from the statement
MultiMath({add, 3, 4}).
{add, 7}.
----
Can we write a non-anonymous function in our module that behaves like the MultiMath anonymous function?
Yes,
it would look like this.
multimath({add, X, Y}) ->
{add, X + Y};
multimath({double, X}) ->
{double, X * 2}.
----
Is it true that we can add the multimath/1 function to our module and have it available for use right away?
False,
first we need to add it to our -export() definition.
----
What will our -export() definition look like now?
-export([double/1, add/2, multimath/1]).
----
Why does multimath have an arity of 1?
Because it only takes 1 argument, a tuple
----
The entire module now looks like this
-module(little_erlanger).
-export([double/1, add/2, multimath/1]).
double(X) ->
X * 2.
add(X, Y) ->
X + Y.
multimath({add, X, Y}) ->
{add, X + Y};
multimath({double, X}) ->
{double, X * 2}.
----
Time for a break!