# Apologies for bad formatting: lecture slides etc.

38 messages
12
Open this post in threaded view
|

## Apologies for bad formatting: lecture slides etc.

 Hi, I'm not getting some of the examples in the slides       t = table.pack(1, nil, 3)       for i = 1, t.n do       print(t[i])       end Does t.n give the value of' 3?      for k, v in pairs(tab) do What does in do in this line of code?       function derivative(f, dx)       dx = dx or 1e - 4       return       function (x)       return(f(x + dx) - f(x)) / dx       end       end I get an error message running this, from the lecture'"more on function". Do resources like this mean I might one day use CSound in lua interpreted by C++ without knowing C? github.com/csound/csoundAPI_examples/tree/master/lua Cheers, Luke
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 More from the documentation... thanks for any help!function map(f)return function (l)  local nl = {}  for i, x in ipairs(l) do  nl[i] = f(x)  end  return nl  endendsquare = map(function (x) return x * x end)print(table.unpack(square{ 1, 5, 9 }))--[[ 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?]]
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 Or perhaps square is not the higher order function: map is its argument not the table? Or is square not a function, even-though it is defined by something (map) that returns a function, as it is not defined with 'function'?On Wed, 7 Nov 2018 at 02:29, Luke <[hidden email]> wrote:More from the documentation... thanks for any help!function map(f)return function (l)  local nl = {}  for i, x in ipairs(l) do  nl[i] = f(x)  end  return nl  endendsquare = map(function (x) return x * x end)print(table.unpack(square{ 1, 5, 9 }))--[[ 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?]]
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 Looks like square is a function that is the return value of the call to map. It takes a single table argument, l, and returns the table nl that contains the squares of the values in l. Does that clear it up?DougOn Tue, Nov 6, 2018, 18:48 Luke <[hidden email]> wrote:Or perhaps square is not the higher order function: map is its argument not the table? Or is square not a function, even-though it is defined by something (map) that returns a function, as it is not defined with 'function'?On Wed, 7 Nov 2018 at 02:29, Luke <[hidden email]> wrote:More from the documentation... thanks for any help!function map(f)return function (l)  local nl = {}  for i, x in ipairs(l) do  nl[i] = f(x)  end  return nl  endendsquare = map(function (x) return x * x end)print(table.unpack(square{ 1, 5, 9 }))--[[ 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?]]
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 In reply to this post by Luke > On Nov 6, 2018, at 9:29 PM, Luke <[hidden email]> 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}
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 > 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 <[hidden email]> wrote: > On Nov 6, 2018, at 9:29 PM, Luke <[hidden email]> 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}
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 In reply to this post by Douglas Valenta why say that it takes the argument l rather than f or rather than x * x?On Wed, 7 Nov 2018 at 02:59, Douglas Valenta <[hidden email]> wrote:Looks like square is a function that is the return value of the call to map. It takes a single table argument, l, and returns the table nl that contains the squares of the values in l. Does that clear it up?DougOn Tue, Nov 6, 2018, 18:48 Luke <[hidden email]> wrote:Or perhaps square is not the higher order function: map is its argument not the table? Or is square not a function, even-though it is defined by something (map) that returns a function, as it is not defined with 'function'?On Wed, 7 Nov 2018 at 02:29, Luke <[hidden email]> wrote:More from the documentation... thanks for any help!function map(f)return function (l)  local nl = {}  for i, x in ipairs(l) do  nl[i] = f(x)  end  return nl  endendsquare = map(function (x) return x * x end)print(table.unpack(square{ 1, 5, 9 }))--[[ 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?]]
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 In reply to this post by Luke Apparently, 1e-4 (it shouldn't have any spaces) is a numerical literal in Lua 5.3, where it is close to 1 * 10^-4, though at least in my Lua 1e-4 is not quite equal to 1 * 10^-4, because the former is roughly 0.000100000000000000004792173602 and the latter is roughly 0.000100000000000000045449755071 . It is apparently not a valid number in Lua 5.1 though, where I get a syntax error "unexpected symbol near 1e-4". So maybe you're using Lua 5.1, not 5.3? 1e - 4 (with spaces around the hyphen) is invalid syntax in both Lua 5.3 and 5.1: "malformed number near '1e' ". — Gabriel On Sun, Nov 4, 2018 at 11:43 PM Luke <[hidden email]> wrote: > > Hi, > > I'm not getting some of the examples in the slides > >       t = table.pack(1, nil, 3) >       for i = 1, t.n do >       print(t[i]) >       end > > Does t.n give the value of' 3? > >      for k, v in pairs(tab) do > > What does in do in this line of code? > >       function derivative(f, dx) >       dx = dx or 1e - 4 >       return >       function (x) >       return(f(x + dx) - f(x)) / dx >       end >       end > > I get an error message running this, from the lecture'"more on function". > > Do resources like this mean I might one day use CSound in lua > interpreted by C++ without knowing C? > > github.com/csound/csoundAPI_examples/tree/master/lua > > Cheers, > Luke >
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 Thanks to everyone. Sorry for being slow and for the mistakes I've made.Best,LukeOn Wed, 7 Nov 2018 at 03:11, Erutuon <[hidden email]> wrote:Apparently, 1e-4 (it shouldn't have any spaces) is a numerical literal in Lua 5.3, where it is close to 1 * 10^-4, though at least in my Lua 1e-4 is not quite equal to 1 * 10^-4, because the former is roughly 0.000100000000000000004792173602 and the latter is roughly 0.000100000000000000045449755071 . It is apparently not a valid number in Lua 5.1 though, where I get a syntax error "unexpected symbol near 1e-4". So maybe you're using Lua 5.1, not 5.3? 1e - 4 (with spaces around the hyphen) is invalid syntax in both Lua 5.3 and 5.1: "malformed number near '1e' ". — Gabriel On Sun, Nov 4, 2018 at 11:43 PM Luke <[hidden email]> wrote: > > Hi, > > I'm not getting some of the examples in the slides > >       t = table.pack(1, nil, 3) >       for i = 1, t.n do >       print(t[i]) >       end > > Does t.n give the value of' 3? > >      for k, v in pairs(tab) do > > What does in do in this line of code? > >       function derivative(f, dx) >       dx = dx or 1e - 4 >       return >       function (x) >       return(f(x + dx) - f(x)) / dx >       end >       end > > I get an error message running this, from the lecture'"more on function". > > Do resources like this mean I might one day use CSound in lua > interpreted by C++ without knowing C? > > github.com/csound/csoundAPI_examples/tree/master/lua > > Cheers, > Luke >
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 In reply to this post by Luke function square = 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).— GabrielOn Tue, Nov 6, 2018 at 9:05 PM Luke <[hidden email]> 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 <[hidden email]> wrote: > On Nov 6, 2018, at 9:29 PM, Luke <[hidden email]> 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}
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 In reply to this post by Luke On Nov 6, 2018, at 10:05 PM, Luke <[hidden email]> 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) Because the function is *already* defined from map. All we need is store this function object into a variable, name "square" (besides, above is not valid lua code) You can also think like this: function add1(x) return x+1 end is same as: add1 = function(x) return x+1 end
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 In reply to this post by Gabriel Bertilson yeah, thanks, I got that.function f()return 3 endthing = fprint(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 <[hidden email]> wrote:function square = 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).— GabrielOn Tue, Nov 6, 2018 at 9:05 PM Luke <[hidden email]> 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 <[hidden email]> wrote: > On Nov 6, 2018, at 9:29 PM, Luke <[hidden email]> 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}
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 Is it right to say that we call a function when it appears in a statement with parentheses after it? I'm also a little confused about the difference between a variable and the value, e.g. if they can be of a different type? Sorry to go on: I just have zero experience of programming.. The reference manual is unreadable in places, as yet. So I'm switching between the lecture notes and book. Hopefully I won't have to keep asking questions to the list. I had similar issues reading the manual for ubuntu, and had to give up. I wonder if there's a webresource etc. that includes a glossary necessary to understand manuals etc. Cheers, Luke On Wed, 7 Nov 2018 at 03:27, Luke <[hidden email]> wrote: > > 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 <[hidden email]> wrote: >> >> function square = 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 <[hidden email]> 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 <[hidden email]> wrote: >>>> >>>> >>>> > On Nov 6, 2018, at 9:29 PM, Luke <[hidden email]> 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} >>>> >>>> >>>> >>>> >>>>
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 On Wed, Nov 7, 2018 at 4:02 PM Luke <[hidden email]> wrote: > I wonder if there's a webresource etc. > that includes a glossary necessary to understand manuals etc. .
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 Thanks. I wonder why no definition of 'operator' Cheers On Thu, 8 Nov 2018 at 00:16, Paul Merrell <[hidden email]> wrote: > > On Wed, Nov 7, 2018 at 4:02 PM Luke <[hidden email]> wrote: > > > I wonder if there's a webresource etc. > > that includes a glossary necessary to understand manuals etc. > > . >
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 And what brackets mean in the reference manual's list of basic functions e.g. assert (v [, message]) Luke On Thu, 8 Nov 2018 at 00:26, Luke <[hidden email]> wrote: > > Thanks. I wonder why no definition of 'operator' > Cheers > On Thu, 8 Nov 2018 at 00:16, Paul Merrell <[hidden email]> wrote: > > > > On Wed, Nov 7, 2018 at 4:02 PM Luke <[hidden email]> wrote: > > > > > I wonder if there's a webresource etc. > > > that includes a glossary necessary to understand manuals etc. > > > > . > >
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 Brackets mean that something is optional. In this case, you can call the function with one or two arguments: `assert(v)` or `assert(v, message)`. — Gabriel On Wed, Nov 7, 2018 at 6:33 PM Luke <[hidden email]> wrote: > > And what brackets mean in the reference manual's list of basic functions e.g. > > assert (v [, message]) > > Luke > On Thu, 8 Nov 2018 at 00:26, Luke <[hidden email]> wrote: > > > > Thanks. I wonder why no definition of 'operator' > > Cheers > > On Thu, 8 Nov 2018 at 00:16, Paul Merrell <[hidden email]> wrote: > > > > > > On Wed, Nov 7, 2018 at 4:02 PM Luke <[hidden email]> wrote: > > > > > > > I wonder if there's a webresource etc. > > > > that includes a glossary necessary to understand manuals etc. > > > > > > . > > > >
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 Thanks again. Is anything else needed available in order to understand the reference manual? e.g. I found out that ::= means is defined as (I assume backwards) but much of the following is beyond me prefixexp ::= var | functioncall | ‘(’ exp ‘)’ On Thu, 8 Nov 2018 at 00:36, Gabriel Bertilson <[hidden email]> wrote: > > Brackets mean that something is optional. In this case, you can call > the function with one or two arguments: `assert(v)` or `assert(v, > message)`. > > — Gabriel > > > On Wed, Nov 7, 2018 at 6:33 PM Luke <[hidden email]> wrote: > > > > And what brackets mean in the reference manual's list of basic functions e.g. > > > > assert (v [, message]) > > > > Luke > > On Thu, 8 Nov 2018 at 00:26, Luke <[hidden email]> wrote: > > > > > > Thanks. I wonder why no definition of 'operator' > > > Cheers > > > On Thu, 8 Nov 2018 at 00:16, Paul Merrell <[hidden email]> wrote: > > > > > > > > On Wed, Nov 7, 2018 at 4:02 PM Luke <[hidden email]> wrote: > > > > > > > > > I wonder if there's a webresource etc. > > > > > that includes a glossary necessary to understand manuals etc. > > > > > > > > . > > > > > > >
Open this post in threaded view
|

## Re: Apologies for bad formatting: lecture slides etc.

 On Wed, Nov 7, 2018 at 7:41 PM Luke <[hidden email]> wrote:Thanks again. Is anything else needed available in order to understand the reference manual? e.g. I found out that ::= means is defined as (I assume backwards) but much of the following is beyond me prefixexp ::= var | functioncall | ‘(’ exp ‘)’That is the formal grammar, which a beginner like you need not worry about. The grammar is intended more for experts.In general, the reference manual is designed to be a concise definition of the language and assumes a certain level of programming knowledge, including some familiarity with Lua itself. It is not a good place for a beginner to learn from.I recommend obtaining the Programming in Lua book; it is written by the Lua team and is much more user- and beginner-friendly, with longer discussions of the topics and many examples. The first edition of the book is available on lua.org, but was written for Lua 5.0. Lua has undergone several major revisions and changes since then, so some of the material in that version is outdated. The fourth edition of Programming in Lua covers Lua 5.3 (the latest release of Lua) and is available on Amazon and possibly other places. It is well worth the cost, especially if you are trying to learn.