lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


yeah, thanks, I got that.

function f()
return 3 end
thing = f
print(type(thing), thing())

thing is a function, but I was confused that you needed to add the parentheses to get print to print its value.

I guess syntax just is the way it is?



On Wed, 7 Nov 2018 at 03:16, Erutuon <arboreous.philologist@gmail.com> wrote:
function square = <something or other> is not valid Lua syntax. "function" has to be followed by (at minimum) parentheses and then "end": function () end. See the section in the Lua manual on function definitions (https://www.lua.org/manual/5.3/manual.html#3.4.11).

— Gabriel



On Tue, Nov 6, 2018 at 9:05 PM Luke <lemmett81@gmail.com> wrote:
> Think of map as a function maker.
After making the function, the job of map is done.
So, map never see the list {1, 5, 9}

That makes sense. Why don't we use function to define square?

function square = map(function (x) return x * x end)

On Wed, 7 Nov 2018 at 02:59, Albert Chan <albertmcchan@yahoo.com> wrote:

> On Nov 6, 2018, at 9:29 PM, Luke <lemmett81@gmail.com> wrote:
>
> I believe that, because the definition does not begin with function, square is an anonymous function; it returns a value and is defined in an _expression_. So, I start with the table { 1, 5, 9 }. This is the argument to the square function. I'm guessing that the map function is called by square. But how / why does map get the table, when map only has one parameter, a function, and the table is not assigned to a variable? And how / why does map return a table value, as it seems to want to return a function?

Think of map as a function maker.
After making the function, the job of map is done.
So, map never see the list {1, 5, 9}

square stored what map function maker created.
square is now a function, that map x to x*x

square( {1,5,9} ) ==> {1,25,81}