Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Right I would say and argue that lexical scope is the main thing that makes Functional Programming languages Functional Programming languages.

Why? Because it allows a function to create and return a new different instance of a function-skeleton coded in the inner scope, part of which function-instance is the values of the variables in its outer scope which were the values of those variables at the time the outer function returned.

That means it is easy to create new function-instances. Function instances are "first class citizens" as much part of the machinery of such language as any data-structure.

In this sense the "inner function" coded inside an outer is not a function at all. It is just an encoding, a set of instructions for creating a new function which instructions are executed to create a new function when you execute the outer function.

Whereas the outermost function is a function by itself since there can be only one instance of it ever.



In practice the inner function (the body of a lexical closure) is only compiled once. When a "new function is made" at runtime, a data structure for the new lexical environment is created together with a pointer to the already-compiled closure body. So the executable portion (the actual instructions of the closure) are only allocated once.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: