Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Functional > Re: A question ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 2 Topic 2831 of 2841
Post > Topic >>

Re: A question (confusion) about closure

by "xahlee@[EMAIL PROTECTED] " <xahlee@[EMAIL PROTECTED] > May 2, 2008 at 02:02 AM

There is no universal definition of the concept of closure in the
context of programing languages.

Basically, if your language can define a function, and maitain a
permanent internal variable inside, it's closure.

For example, if you define a global var, and your function use access
and or set that global var, your function will be able to maintain a
state.

However, we all know global vars are bad because, for example, you
have this big pool where everyone and every function accesses... it's
hard to maitain and could be over-ridden by mistake...

So, a crude way to remedy this, is by following a convention, such
that any global var who's name starts with a 2 underscore is meant to
for special use. So, this way, you can have global vars __this ,
__that , __x, etc, and your function can call them. Now your function
can maintain a state, while the typical problem with using global vars
is avoided by a naming convention.

Now, effectively, your function is said to have =E2=80=9Cclosure=E2=80=9D.
T=
ypically,
that word is used when the language provide means to allow function to
have a state without the above hackish method. However, it's no more
complicated than that. In fact, it is effectively the only means to
implement closure. If the language hides such global vars from user,
and provide transparent ways when you create or access them inside
your function without the __ prefix, that's closure.

Once a functional language's funtion's has states, that is, having
closure, it is natural to extend the concept to become
=E2=80=9Cobjects=E2=
=80=9D as in
Object Oriented Programing.

When you start to program in a way of closures, that is, a function
uses permant internal variables to maintain states, and you have many
functions like that, naturally, you might systematically explore this
paradigm. Namely, instead of just one or two permanent internal
variables, each of your function uses tens of such permanent internal
vars. This is the starting concept of so-called Object Oriented
Programing.

For detail and further explanation, see:

What are OOP's Jargons and Complexities
 http://xahlee.org/Periodic_dosage_dir/t2/oop.html

--------------

Note: The terminology =E2=80=9CClosure=E2=80=9D is actually a very bad
termi=
nology. It
spreads endless confusion and non-understanding. See:

Jargons of Info Tech industry
http://xahlee.org/UnixResource_dir/writ/jargons.html

Math Terminology and Naming of Things
http://xahlee.org/cmaci/notation/math_namings.html

  Xah
  xah@[EMAIL PROTECTED]
 http://xahlee.org/

=E2=98=84

On May 2, 1:31 am, Samik <sam...@[EMAIL PROTECTED]
> wrote:
> Hi,
>
> I have a confusion regarding closure and its definition. As per Paul
> Graham:
>
> When a function refers to a variable defined outside it, it=E2=80=99s
call=
ed a
> free variable. A function that refers to free lexical variable is
> called a closure.
>
> However as per CLHS a closure or lexical closure is:
>
> lexical closure n. a function that, when invoked on arguments,
> executes the body of a lambda
> expression in the lexical environment that was captured at the time of
> the creation of the lexical closure, augmented by bindings of the
> function's parameters to the corresponding arguments.
>
> lexical environment n. that part of the environment that contains
> bindings whose names have lexical scope. A lexical environment
> contains, among other things: ordinary bindings of variable names to
> values, lexically established bindings of function names to functions,
> macros, symbol macros, blocks, tags, and local declarations (see
> declare).
>
> Can any pro lisper kindly help me?
>
> Thanks,
> Samik
> Blog:http://lispsamik.blogspot.com/




 2 Posts in Topic:
Re: A question (confusion) about closure
"xahlee@[EMAIL PROTE  2008-05-02 02:02:14 
Re: A question (confusion) about closure
lisp2.3.CalRobert@[EMAIL   2008-05-04 02:44:05 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon May 12 0:49:09 CDT 2008.