Write a function that takes arbitrarily many numeric parameters and returns the sum and product of these numbers.
Write a function that takes the number of a zone (in the currently open model) and returns that zone's name. Implement your function so that it follows the Lua convention for error reporting, i.e. if an error occurs, return two values, nil and an error message. (For example, an error might occur if you ask for zone 10 and there are only 5 zones.)
Write a function that returns a function. In particular, write a function make_adder that takes a number as input and returns a function that behaves as follows: takes a number as input and returns that number plus its specific value. Example:
plus_five = make_adder(5) minus_two = make_adder(-2) print(plus_five(4)) -- outputs 9 print(plus_five(0)) -- outputs 5 print(minus_two(4)) -- outputs 2 print(minus_two(plus_five(3))) -- outputs 6
Write two functions, fun1 and fun2, that share the same upvalue. Have fun1 print the upvalue and fun2 print twice the upvalue.
There are two ways to do this. The first is to write a function fun_factory which creates and returns the two functions. The shared upvalue will be a variable that is local to fun_factory. The second method is to wrap the creation of fun1 and fun2 in do/end. The shared upvalue will be local to the block.
Modify the previous exercise by adding a third function, fun3, which shares the same upvalue. However, fun3 should let the user change the shared upvalue. Note that in Lua 5.x this takes no special arrangements. In Lua 4, however, typically upvalues are read-only. The way around this is to make the upvalue a table. The functions sharing this upvalue cannot change which table is referenced, but can modify entries in the table.