APL return value of a function - return

I want to know how to return a value after my function finishes running.
I have, for example:
FUNCTION X
? X ⍴ 10
//This means, generate X random numbers (X is the function's argument) within the range 1-10.
I just want to know how I can return the value of the function, and for example, pass it to another function.
Thank you for your help!

This is done in the Function header (code line 0).
It has the following form:
returnValue ← leftArgument functionName rightArgument ; localized_variables
So, whenever your function terminates, the value of the variable returnValue will be returned.

Alternately, if your APL system supports it, you can use direct definition (dfns, lambdas). This should work in Dyalog, GNU, NARS2000, and NGN APL.
Try
{?⍵⍴10} 42
⍵ is the function's argument (X in your example)
The return value is the result of the expression and does not have to be explicitly stated.
You can also do
function←{?⍵⍴10}
then
function 42

Related

understand mutable variable closure

In this example:
let generateStamp =
let mutable count = 0
(fun () -> count <- count + 1; count)
generateStamp ()
generateStamp ()
// prints 1 and 2
Isn't mutable count allocated for every generateStamp() call ? If that happens you would have to store the result of that call in order to be able to increment the counter, but that does not seem to be the correct assumption here.
No. You've defined generateStamp to be a value, so it's only evaluated once, binding the name generateStamp to a lambda of type (unit -> int). Every invocation of that lambda then shares the same count variable.
If you had defined it as a plain function instead, every invocation would allocate its own count, like this:
let generateStamp () =
let mutable count = 0
count <- count + 1; count
generateStamp () // 1
generateStamp () // also 1
Note that the output is only different because generateStamp is impure. For pure functions (i.e. no side-effects), the output of the two versions would always be the same (although one might be faster than the other).
Also, note that the type signature is slightly different for the two versions. The first one is (unit -> int), while the second one is unit -> int. (I'm not sure if this is anything more than a quirk of the F# compiler, but it's interesting to be aware of nonetheless.)
No. An amateur (in this regard) tries to explain:
You are not calling a function named generateStamp. generateStamp has a value which is a function, expressed as a lambda. generateStamp is calculated once before you call the function it has. Part of that calculation is to set count to zero. So that happens only once.
The expression generateStamp () means that you first retrieve a function with the expression generateStamp, however which way that comes about - meaning it could be a declared function or a computed value as in this case. Then you supply an argument to make it execute.
The expression generateStamp () looks just like a normal function call, unlike in C# where you have to deal with this sort of thing in a much more cumbersome way. When we talk about functions being first-class members of the language, this is part of what it is about.

How can a comma-separated return statement in Lua act as a function call?

I'm new to Lua and trying to figure out how the return statement in the squares function below is being used in the following code snippet:
function squares(iteratorMaxCount)
return square,iteratorMaxCount,0
end
The square parameter in the return statement refers to a function with the following signature:
function square(iteratorMaxCount,currentNumber)
What's confusing me is that the return statement looks like it's returning three values. What I think it's actually doing, however, is passing iteratorMaxCount and 0 as the arguments to a square function call.
Can anyone explain to me what's happening with this syntax? How is this serving as a function call as opposed to returning three values? In my mind, it feels as though the return statement should be written return square(iteratorMaxCount, 0) as opposed to return square, iteratorMaxCount, 0. I know that this is obviously wrong, but I can't figure out why.
I've tried searching through the Lua Manual, Lua Reference Guide, and searching Google, but I can't seem to find anything that explains this particular syntax. Can anyone point me in the right direction, please?
Thanks in advance.
Full code below via
Tutorialspoint
function square(iteratorMaxCount,currentNumber)
if currentNumber<iteratorMaxCount
then
currentNumber = currentNumber+1
return currentNumber, currentNumber*currentNumber
end
end
function squares(iteratorMaxCount)
return square,iteratorMaxCount,0
end
for i,n in squares(3)
do
print(i,n)
end
squares really does return three values, the first of which is a function. squares does not call square at all.
The trick here is how the for ... in syntax works. In the Lua 5.3 Reference Manual, section 3.3.5 says:
A for statement like:
for var_1, ···, var_n in explist do block end
is equivalent to the code:
do
local f, s, var = explist
while true do
local var_1, ···, var_n = f(s, var)
if var_1 == nil then break end
var = var_1
block
end
end
So the keyword "in" needs to be followed by three values:
an "iterator function" for getting the variables in each iteration
a "state" value to pass to the function each time
an initial value to pass to the function the first time
After the first time the function is called, the first value from the previous call is passed back into the next function call. When the first value returned from the function is nil, the for loop ends.
So in this example, squares(max) is designed to be used after "in", using square as the iterator function, max as the "state", 0 as the initial value, and a number and its square as the loop data values.

Lua table.insert does not accept a string parameter

Continuing to learn Lua.
I have wrote a function that removes the first sentence from each line and returns the result as a table of modified lines, where the first sentence was removed. Strangely, table.insert behaves weird in such function.
function mypackage.remove_first(table_of_lines)
local lns = table_of_lines
local new_lns = {}
for i=1,#lns do
table.insert(new_lns,string.gsub(lns[i],"^[^.]+. ","",1))
end
return new_lns
end
Unexpectedly, this gave me the following error.
[string "function mypackage.remove_first(table_of_lines)..."]:5: bad argument #2 to 'insert' (number expected, got string)
Why is "number expected" in the first place?
From table.insert docs
Inserts element value at position pos in list, shifting up the
elements list[pos], list[pos+1], ···, list[#list]. The default value
for pos is #list+1, so that a call table.insert(t,x) inserts x at the
end of list t.
Nothing is said about type requirements for table.insert. Ok, I decided to modify the example.
function mypackage.remove_first(table_of_lines)
local lns = table_of_lines
local new_lns = {}
for i=1,#lns do
local nofirst = string.gsub(lns[i],"^[^.]+. ","",1)
table.insert(new_lns,nofirst)
end
return new_lns
end
And now everything works. Can you explain what is going on here?
The problem is a bit complicated. It's a collision of three factors:
string.gsub returns two parameters; the second parameter is the number of matches.
table.insert can take 3 parameters. When it is given 3 parameters, the second parameter is expected to be an integer offset defining where to insert the object.
When you do this: func1(func2()), all of the return values of func2 are passed to func1, so long as you don't pass arguments after func2 in func1's argument list. So func1(func2(), something_else) will get only 2 arguments.
Therefore, when you do table.insert(ins, string.gsub(...)), this will invoke the 3-argument version, which expects the second argument to be the index to insert the object into. Hence the problem.
If you want to ensure discarding, then you can wrap the expression in parenthesis:
table.insert(new_lns, (string.gsub(lns[i], "^[^.]+. ", "", 1)))

javascript: closures, anonymous functions, iife

A friend asked me to design a function that does the following : f1()()()()(0)
should give the output as 4. f1()(0) should give output as 1. It is the number of preceding parentheses before 0 is passed. I searched thoroughly on how it should be done. Got some concepts to string together: like IIFE, Anonymous Functions and Lexical Scope. Does this use a more advanced javascript concept function that should be known?
Here is the image of what needs to be done
No advanced concepts. You only need to know how to return a function from another function.
I would prefer to call the function f0 because f0(0) obviously should return 0. Then we can imagine all such kinds of functions that return their level of parenthetication—for example, f42(0) returns 42.
When called without a parameter, f0() should return f1, so that f0()(0) is 1, and so on. That is an easy thing to do:
function f0(x) {
return (x === 0) ? 0 : f1(x);
}
Obviously, we do not want to write down an infinity of functions like this. Let's make a function factory that will build them automatically as needed:
function factory(level) {
function f_level(x) {
return (x === 0) ? level : factory(level + 1);
}
return f_level;
}
The factory always returns a function, and factory(0) is exactly the function f0 you wanted.

Function with other function as argument

Please, tell me how use functions in Maxima?
I tried this
function(g, u):= (print(g(0)), print(u));
function(x^2, 10);
but it doesn't work
I guess you want to evaluate the first argument with a specific value of the second argument. So maybe you want ev(g, x=0) instead of g(0).
(g(0) works only if g is the name of a function or a lambda expression, i.e., an unnamed function.)
Maybe you can explain in more detail what you want to accomplish.
When defining a function F(Y) you want Y to be a x-dependent input variable for the former, right?
You can directly define the a function F of unknown variable Y and then input some function as argument.
(%i_) F(Y) := diff(Y(t),t) + Y(t^2);
F(sin);
(%o_) F(Y):='diff(Y(t),t,1)+Y(t^2)
(%o_) sin(t^2)+cos(t)
and you are done!
If you try to parse F(foo(x)):=something + foo(x) you get
define: in definition of F, found bad argument foo(x)
There is a more specific way I took from maxima: use function as function argument:
Before defining F(foo), you can tell maxima
"by foo I mean foo(x)": depends(foo,x).
Then you can define a function with variable foo, for example
depends(foo,x);
F(foo) := foo(x^2) + foo(x) + diff(foo(x),x) ;
F(sin);

Resources