Deadly Boss Mods

It is currently Thu Sep 09, 2010 10:42 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Learning Lua from your book
PostPosted: Sat Jul 03, 2010 10:14 am 
Offline

Joined: Sat Jul 03, 2010 9:21 am
Posts: 2
Hi all,

Well hopefully you'll forgive my dumbness to lua for the time being and are understanding that to learn i have to start somewhere. Having played wow for a fair few years now and getting tired of my favourite addons getting broken and stopped being updated, plus the fact i can never find the addon i would like lol. I have decided to broaden my horizon and learn lua through your book.

Now for the dumb questions to start if nobody here minds that is. I like to understand what i am learning (doesnt make sense but bare with me plz).

In chapter 2 the code calculating the factorial of a number (below) i have been trying to break it down so i can understand it better to follow. Whether this is a good idea or not i dont know but i was having trouble following the book further in so thought i'd go from the start again. Anyway :

Code:
function fac(n)
    if n == 0 then
       return 1 -- so by my thinking the first time this part is ran n will always be 0
else
    return n * fac(n - 1) -- from the for part of this code further down when the code is looped here after i.e 2nd time function fac(n) is ran it will be 1 because of the return 1 in the 3rd line and the return changes to the outcome of n *fac(n - 1)
end
end

for i = 1, 10 do
print(fac(i)) -- so this runs the loop 10 time altering (n) as it is called


If i try and make this code down on paper and follow it i cant make the same numbers as the code does, the only thing i can think of is (i) is being inserted in the code somewhere to alter the value of (n) depending on the 1, 10 part it's at. Basically can someone explain the last 2 lines for me plz.

As i said im still learning and may be looking at this all the wrong way but it's always good to bounce stuff off other peoples heads instead of giving up. I have a reference book on it's way from amazon lol hoping it will help.

If anyone could take the time to help me learn with the dumb q's i have it would be most appreciative.

Thanks


Top
 Profile  
 
 Post subject: Re: Learning Lua from your book
PostPosted: Sat Jul 03, 2010 10:29 am 
Offline
Site Admin

Joined: Fri Mar 16, 2007 6:55 pm
Posts: 1518
The fac(n) function:

Let's say you call fac(n) with n = 4:

fac(4)
--> n = 4 != 0, so fac(4) returns
4 * fac(3)

--> n = 3 != 0, so fac(3) returns
4 * fac(2)

--> n = 2 != 0, so fac(2) returns
2 * fac(1)

--> n = 1 != 0, so fac(1) returns
1 * fac(0)

--> n = 0, so fac(0) returns
1

fac(0) returns to the call from fac(1), which now knows fac(0) and returns 1 * fac(0) = 1 * 1 = 1
the function then returns to the call from fac(2), which now knows that fac(1) is 1 and returns 2 * fac(1) = 2 * 1 = 2
the function then returns to the call from fac(3), which now knows that fac(2) is 2 and returns 3 * fac(2) = 3 * 2 = 6
the function then returns to the call from fac(4), which now knows that fac(3) is 6 and returns 4 * fac(3) = 4 * 6 = 24

fac(4) is the last function on the call stack, so the overall result is 24


This function is now called in the last 3 lines
for i = 1, 10 do
print(fac(i))
end

This loop just calls fac with the numbers 1 to 10 and prints the results


Top
 Profile  
 
 Post subject: Re: Learning Lua from your book
PostPosted: Sat Jul 03, 2010 12:00 pm 
Offline

Joined: Sat Jul 03, 2010 9:21 am
Posts: 2
Great, thanks alot Tandanu.
Still a little confused but not as much as before :? Instead of making new topics all the time i'll keep all the q's to this thread.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group