Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK Beginning Lua Programming ISBN: 978-0-470-06917-2
This is the forum to discuss the Wrox book Beginning Lua Programming by Kurt Jung, Aaron Brown; ISBN: 9780470069172
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK Beginning Lua Programming ISBN: 978-0-470-06917-2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old November 17th, 2010, 12:14 AM
Registered User
 
Join Date: Nov 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question Chapter 3, Exercise 5 confusion

I'm completely new to Lua and relatively new to programming, not having done more than BASIC, HyperCard, AppleScript, etc. I'm working my way through the book, but I'm stuck on Exercise 5 in Chapter3, page 114. Here it is:
Code:
-- Returns a function that appends N dots to its argument:
function MakeDotter(N)
  Dots = ""
  for I = 1, N do
    Dots = Dots .. "."
  end
  return function(Str)
    return Str .. Dots
  end
end
The question, of course, is What is wrong with this program? The answer the book gives is that Dots is not local. What I don't see is why. True, when you follow the book and do
Code:
OneDotter=MakeDotter(1)
print(OneDotter("A"))
you get
Code:
A.
and when you do
Code:
ThreeDotter=MakeDotter(3)
print(ThreeDotter("B"))
you get
Code:
B...
and then if you do
Code:
print(OneDotter("C"))
you get
Code:
C...
.

But I don't see what that has to do with Dots. Because if you then do
Code:
print(MakeDotter(1)("D"))
you get
Code:
D.
just like you should. It seems to me that the problem is with N, not Dots. After all, the value of Dots gets reset to "" --blank--every time the function is called.

Now I'm aware--after trying it with the Lua interpreter at <http://www.lua.org/cgi-bin/demo> (since there is no functional Lua interpeter for Windows Mobile), that making Dots local fixes the problem. What I don't know is WHY.

Help please?
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exercise 6 for chapter 2 scooter5 BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 3 August 27th, 2010 11:24 AM
Chapter 3 exercise 2 weekapaugh BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 2 March 11th, 2010 01:28 AM
Chapter 8 exercise 3 Will BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 8 March 4th, 2010 03:15 AM
Chapter 3 - Exercise 3 AndyN BOOK: Beginning Cryptography with Java 3 August 16th, 2006 03:09 PM
Chapter 4, Exercise 3 DRAYKKO BOOK: Beginning Java 2 3 July 9th, 2004 02:34 PM



All times are GMT -4. The time now is 02:23 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.