Wrox Programmer Forums
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 17th, 2010, 01: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?




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 02: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 04: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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.