1. (6 points) Show how to write the following piece of XML as a short string and as a long string:
Answer:
2. (6 points) After running a = {}; a.a = a
, what is the
value of a.a.a.a
? If we now do a.a.a.a = 3
, what is the
value of a.a.a.a
?
3. (6 points) Write a function that receives an arbitrary number of values and returns all of them, except the first one.
4. (12 points) Write a function poly
that takes a polynomial represented
as an array of coeficients and returns a function that takes a
parameter x
and evaluates the polynomial for x
. For example:
Answer:
5. (15 points) Write a function fibs
that returns a closure iterator
that produces the first n
numbers
of the Fibonacci sequence (1, 1, 2, 3, 5, 8, 13, …). For example:
Answer:
6. (6 points) Suppose the value of package.path
is .\?.lua;c:\Lua52\libs\?.lua;c:\Lua52\libs\?\init.lua
.
What files will Lua try to load if we do require "mypack.mymod"
?
7. (6 points) Is the operation ==
always commutative in Lua? Explain.
8. (15 points) Write an Iterator
class that takes a closure iterator
in its constructor. Objects of the Iterator
class have a next
method
that returns the next element produced by the closure iterator.
9. (6 points) Assume the Lua stack is empty. What will be its contents after the following sequences of calls in a C function?
Answer:
10. (10 points) A web server uses a Lua configuration file like the one below.
Assume that the configuration file has already been loaded. Write the C code that
extracts the configuration values from the Lua state, and writes them to variables
addr_ip
, addr_port
, and http_root
on the C side.
Answer:
11. (6 points) Write a C function that receives an arbitrary number of values and returns all of them, except the first one.
12. (6 points) Write the Lua code that corresponds to the sequence
of Lua VM instructions below. Assume that register 0 is the local
variable a
, register 1 is b
, and register 2 is c
.
Answer (void):