Showing posts with label programming skill development. Show all posts
Showing posts with label programming skill development. Show all posts

Wednesday, February 28, 2007

New blog about programming

Because this blog is about scripting Anime Studio Pro, I thought it best to create a new blog that tries to capture what I'm doing to learn computer programming. Putting the posts in this blog wouldn't be fair for those who do not want to learn those other languages, and just concentrate on Lua and Anime Studio.

The new blog is called (no surprise there): I want to write programs.

Tuesday, February 27, 2007

Closure - 3

Wow, I finally got the concept of closure. I got it from reading Wikipedia - Scope (Programming)

Suppose you have this piece of code:

x = 0

do
  local x = 0
  function f () return x end
  function g () x = x + 1; return f() end
end

print(x)    -- 0
print(f() ) -- 0
print(g() ) -- 1
print(x)    -- 0

x = 5
print(x)    -- 5
print(f() ) -- 1
print(g() ) -- 2
print(x)    -- 5

The first occurrence of x is a global variable, which is dynamic; it can be changed by an assignment anywhere else in the program.

However, the second occurrence of x is something completely different. It is inside a do ... end construct (a chunk in Lua jargon), and it is defined local in that chunk. It cannot be accessed outside this chuck. Nevertheless, through the functions f and g the value of the x inside the do ... end construct can be accessed. It can be looked up in the script code, and, therefore, can be called a "lexical variable", because the scope of the variable is lexical (determined by executable code--functions--in the script). Remember, the functions f and g can be accessed outside the do ... end construct, because they are defined as global.

So a closure is a global function and its "lexical variables" (upvalues in Lua jargon), i.e. variables that can only be accessed through the function of the closure.

Now, suppose you had used a global variable x inside the do ... end construct:

x = 0

do
  x = 0
  function f () return x end
  function g () x = x + 1; return f() end
end

print(x)    -- 0
print(f() ) -- 0
print(g() ) -- 1
print(x)    -- 1

x = 5
print(x)    -- 5
print(f() ) -- 5
print(g() ) -- 6
print(x)    -- 6

Now there is only one and the same variable x, and there is no upvalue in this piece of code. Therefore, neither f nor g are closures.

Crash course programming

Well, I suppose like many of the readers of this blog, I never had a formal introduction into programming. Perhaps it is not so bad to take that step back, and do some programming basics.

The programming course using How to Design Programs and its programming environment DrScheme, is actually quite good. It start really simple with calculus, and progresses gently to more difficult topics. The programming environment is made more complex along with the increasing complexity of the course material. DrScheme has several language settings, which hide certain language constructs by making them invalid. In fact, this is a tailored teaching tool for anyone who wants to learn how to program, develop the skills and learn the discipline and methods how to solve problems.

If you haven't received a formal training in computer science, and you have no access to a classroom course, this is the next best thing for study. It isn't glamorous, but I think it is necessary to understand the basics of programming before attempting to write Lua scripts for Anime Studio.

Sunday, February 25, 2007

Closure - 2

I'm beginning to understand what closure is. I cannot yet express it in words, but I can picture it in my mind. I've used Wikipedia as a reference, and it has some good articles on computer science (at least, the English language version at en.wikipedia.org).

Now, the text in Chapter 6.1 of Programming in Lua makes sense, while it did not before I studied the Wikipedia articles.