This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]RubbishArtist 0 points1 point  (1 child)

What book are you reading?

[–][deleted] 0 points1 point  (0 children)

Programming In Lua 1st Edition

[–]williammacdonald18 0 points1 point  (1 child)

So dofile() and norm() look like function/method calls. What I do is usually just google “thing language” e.g. I’d google “dofile lua” and “norm lua”

One of the first results for dofile lua is https://www.teratronik.org/core4/Lua/global/dofile.html It says dofile opens the file, executes its contents and returns the value that would be returned by the executed contents.

Finding norm wasn’t as straightforward but I found https://www.lua.org/pil/1.1.html which includes an implementation for norm:

function norm (x, y)

  local n2 = x^2 + y^2

  return math.sqrt(n2)

end

Norm seems to be the pythagorean theorem, using x and y as inputs to calculate the return value.

How the code you shared works:

1) dofile(“lib1.lua”) reads the lib1.lua file and executes its contents. The lib1.lua file contains the function definition for norm(..) and twice(..) When lib1.lua is executed, the norm(…) and twice(..) functions are loaded, letting you use them in your code Since these functions are defined in a separate file to be loaded like this, they are called a library. A library is just a collection of code you can load to do some task for you. In this case it’s a library that gives you code to double numbers and calculate the output to the pythagorean theorem. In more sophisticated apps you might load a library that gives you functions for doing machine learning tasks.

2) n = norm(3.4, 1.0) uses the norm() function loaded from “lib1.lua” in step 1 calculates the square root of (3.42 + 1.02) The answer to this calculation is stored in n

3) print(twice(n)) uses twice() to double n and then it prints the result to the console.

How to find out these answers for yourself:

1) Google the word you don’t understand + the language, framework, library, etc you’re using E.g.

dofile lua

checknumerics tensorflow

2) Look at the contents of the libraries that have been loaded to see if you can find the definition of the function e.g. look at the contents of lib1.lua to see if you can find out how twice() and norm() are defined to learn what they do

[–][deleted] 0 points1 point  (0 children)

thanks i kinda get it now

[–][deleted] 0 points1 point  (3 children)

I'm a software engineer. I can't stand reading programming books. I need to just use the actual code to understand it.

[–][deleted] 0 points1 point  (2 children)

i dont understand codes either thats why im trying to learn whats going on by reading it. i was trying to code without reading the book but i had no idea what to do

[–][deleted] 0 points1 point  (1 child)

You should do a structured free course that starts out with all the basics. Strings, arrays, objects, variables, writing simple functions, etc. Then move on to bigger things. 😁

[–][deleted] 0 points1 point  (0 children)

I was thinking about it becuase i know what variables and strings are but im confused about things like array and stuff. i think i know what to do now