Anonymous user
Nested function: Difference between revisions
→{{header|zkl}}: text
(→{{header|Lua}}: added zkl) |
(→{{header|zkl}}: text) |
||
Line 63:
=={{header|zkl}}==
zkl functions don't have direct access to another functions scope, they are not nested. If a function is defined in another function, the compiler moves it out and hands you a reference to the function. So, you are unable to modify variables in the enclosing scope unless you are given a container which can be modified. Partial application can be used to bind [copies] of scope information to a function, that information is fixed at the point of application and becomes strictly local to the binding function (ie changes do not propagate). A Ref[erence] is a container that holds an object so it can be modified by other entities.
<lang zkl>fcn makeList(separator){
counter:=Ref(1); // a container holding a one. A reference.
// 'wrap is partial application, in this case binding counter and separator
makeItem:='wrap(item){ c:=counter.inc(); String(c,separator,item,"\n") };
makeItem("first") + makeItem("second") + makeItem("third")
|