Nested function: Difference between revisions

From Rosetta Code
Content added Content deleted
(Wrote a proper task description)
(J draft)
Line 50: Line 50:


Console.WriteLine(MakeList(". "));</lang>
Console.WriteLine(MakeList(". "));</lang>

=={{header|J}}==

J does not have nested scopes, so they must be emulated. (The design philosophy here is that nesting tends to become difficult to understand when taken too far, so the coder and designer should be mildly penalized with extra work for choosing nesting as opposed to some other problem solving approach.)

That said, emulating a single level of nesting is relatively trivial and does not reflect the complexities necessary for more elaborate (and more difficult to understand) cases:

<lang J>MakeList=: dyad define
sep_MakeList_=: x
cnt_MakeList_=: 0
;MakeItem each y
)

MakeItem=: verb define
cnt_MakeList_=: cnt_MakeList_+1
(":cnt_MakeList_),sep_MakeList_,y,LF
)</lang>

Example use:

<lang J> '. ' MakeList 'first';'second';'third'
1. first
2. second
3. third
</lang>


=={{header|JavaScript}}==
=={{header|JavaScript}}==

Revision as of 22:45, 18 September 2016

Nested function is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

In many languages, functions can be nested, resulting in outer functions and inner functions. The inner function can access variables from the outer function. In most languages, the inner function can also modify variables in the outer function.

The Task

Write a program consisting of two nested functions that prints the following text.

1. first
2. second
3. third

The outer function (called MakeList or equivalent) is responsible for creating the list as a whole and is given the separator ". " as argument. It also defines a counter variable to keep track of the item number. This demonstrates how the inner function can influence the variables in the outer function.

The inner function (called MakeItem or equivalent) is reponsible for creating one of the list items. It accesses the separator from the outer function and modifies the counter.

References:

ALGOL 68

<lang algol68>PROC make list = ( STRING separator )STRING:

    BEGIN
       INT counter := 0;
       PROC make item = ( STRING item )STRING:
            BEGIN
               counter +:= 1;
               whole( counter, 0 ) + separator + item + REPR 10
            END; # make item #
       make item( "first" ) + make item( "second" ) + make item( "third" )
    END; # make list #

print( ( make list( ". " ) ) ) </lang>

C#

<lang csharp>string MakeList(string separator) {

   var counter = 1;
   var makeItem = new Func<string, string>((item) => {
       return counter++ + separator + item + "\n";
   });
   return makeItem("first") + makeItem("second") + makeItem("third");

}

Console.WriteLine(MakeList(". "));</lang>

J

J does not have nested scopes, so they must be emulated. (The design philosophy here is that nesting tends to become difficult to understand when taken too far, so the coder and designer should be mildly penalized with extra work for choosing nesting as opposed to some other problem solving approach.)

That said, emulating a single level of nesting is relatively trivial and does not reflect the complexities necessary for more elaborate (and more difficult to understand) cases:

<lang J>MakeList=: dyad define

 sep_MakeList_=: x
 cnt_MakeList_=: 0
 ;MakeItem each y

)

MakeItem=: verb define

 cnt_MakeList_=: cnt_MakeList_+1
 (":cnt_MakeList_),sep_MakeList_,y,LF

)</lang>

Example use:

<lang J> '. ' MakeList 'first';'second';'third' 1. first 2. second 3. third </lang>

JavaScript

<lang javascript>function makeList(separator) {

 var counter = 1;
 function makeItem(item) {
   return counter++ + separator + item + "\n";
 }
 return makeItem("first") + makeItem("second") + makeItem("third");

}

console.log(makeList(". "));</lang>

Lua

<lang lua>function makeList(separator)

 local counter = 1
 local function makeItem(item)
   return counter .. separator .. item .. "\n"
 end
 return makeItem("first") .. makeItem("second") .. makeItem("third")

end

print(makeList(". "))</lang>

Objective-C

<lang objc>NSString *makeList(NSString *separator) {

 __block int counter = 1;
 
 NSString *(^makeItem)(NSString *) = ^(NSString *item) {
   return [NSString stringWithFormat:@"%d%@%@\n", counter++, separator, item];
 };
 
 return [NSString stringWithFormat:@"%@%@%@", makeItem(@"first"), makeItem(@"second"), makeItem(@"third")];

}

int main() {

 NSLog(@"%@", makeList(@". "));
 return 0;

}</lang>

Perl

<lang perl>sub makeList {

   my $separator = shift;
   my $counter = 1;
   sub makeItem { $counter++ . $separator . shift . "\n" }
   makeItem("first") . makeItem("second") . makeItem("third")

}

print makeList(". ");</lang>

PHP

Works with: PHP version 5.3+

<lang php><? function makeList($separator) {

 $counter = 1;
 $makeItem = function ($item) use ($separator, &$counter) {
   return $counter++ . $separator . $item . "\n";
 };
 return $makeItem("first") . $makeItem("second") . $makeItem("third");

}

echo makeList(". "); ?></lang>

Python

Works with: Python version 3+

<lang python>def makeList(separator):

   counter = 1
   def makeItem(item):
       nonlocal counter
       result = str(counter) + separator + item + "\n"
       counter += 1
       return result
   return makeItem("first") + makeItem("second") + makeItem("third")

print(makeList(". "))</lang>

Scheme

<lang scheme>(define (make-list separator)

 (define counter 1)
 
 (define (make-item item)
   (let ((result (string-append (number->string counter) separator item "\n")))
     (set! counter (+ counter 1))
     result))
 
 (string-append (make-item "first") (make-item "second") (make-item "third")))

(display (make-list ". "))</lang>

Swift

<lang swift>func makeList(_ separator: String) -> String {

 var counter = 1
 
 func makeItem(_ item: String) -> String {
   let result = String(counter) + separator + item + "\n"
   counter += 1
   return result
 }
 
 return makeItem("first") + makeItem("second") + makeItem("third")

}

print(makeList(". "))</lang>

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")

}

print(makeList(". "));</lang>

Output:
1. first
2. second
3. third