Nested function: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 209: Line 209:
(* ****** ****** *)
(* ****** ****** *)
</lang>
</lang>



=={{header|C}}==
=={{header|C}}==
Line 264: Line 263:
</pre>
</pre>


=={{header|C++}}==
=={{header|C sharp|C#}}==
{{works with|C++11}}
<lang cpp>#include <iostream>
#include <string>
#include <vector>
std::vector<std::string> makeList(std::string separator) {
auto counter = 0;
auto makeItem = [&](std::string item) {
return std::to_string(++counter) + separator + item;
};
return {makeItem("first"), makeItem("second"), makeItem("third")};
}

int main() {
for (auto item : makeList(". "))
std::cout << item << "\n";
}</lang>

=={{header|C#}}==
<lang csharp>string MakeList(string separator)
<lang csharp>string MakeList(string separator)
{
{
Line 303: Line 283:
//using string interpolation
//using string interpolation
string MakeItem(string item) => $"{counter++}{separator}{item}\n";
string MakeItem(string item) => $"{counter++}{separator}{item}\n";
}</lang>

=={{header|C++}}==
{{works with|C++11}}
<lang cpp>#include <iostream>
#include <string>
#include <vector>
std::vector<std::string> makeList(std::string separator) {
auto counter = 0;
auto makeItem = [&](std::string item) {
return std::to_string(++counter) + separator + item;
};
return {makeItem("first"), makeItem("second"), makeItem("third")};
}

int main() {
for (auto item : makeList(". "))
std::cout << item << "\n";
}</lang>
}</lang>


Line 426: Line 425:
". " make-list write</lang>
". " make-list write</lang>

=={{header|Fōrmulæ}}==

In [http://wiki.formulae.org/Nested_function this] page you can see the solution of this task.

Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.

The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.


=={{header|Fortran}}==
=={{header|Fortran}}==
Line 515: Line 506:


F95 introduced facilities whereby a string-style compound variable with both content and current length could be defined and manipulated, and when assigned to it would be reallocated storage so as to have exactly the size to hold the result. Later fortran standardised such a scheme. Similarly, one could define a data aggregate containing a count <code>N</code> as well as the <code>TEXT</code> array and a function could return such a compound entity as its result. It may also be possible to arrange that array TEXT becomes "ragged", that is, TEXT(i) is not always 28 characters long, but only as much as is needed to store the actual item.
F95 introduced facilities whereby a string-style compound variable with both content and current length could be defined and manipulated, and when assigned to it would be reallocated storage so as to have exactly the size to hold the result. Later fortran standardised such a scheme. Similarly, one could define a data aggregate containing a count <code>N</code> as well as the <code>TEXT</code> array and a function could return such a compound entity as its result. It may also be possible to arrange that array TEXT becomes "ragged", that is, TEXT(i) is not always 28 characters long, but only as much as is needed to store the actual item.

=={{header|FreeBASIC}}==

FreeBASIC does not currently support either nested procedures or lambda expressions.
The best we can do here is to create two separate procedures but pass the state of the first procedure
by reference to the second procedure so it can be modified by the latter.

<lang freebasic>' FB 1.05.0 Win64

Sub makeItem(sep As String, ByRef counter As Integer, text As String)
counter += 1
Print counter; sep; text
End Sub

Sub makeList(sep As String)
Dim a(0 To 2) As String = {"first", "second", "third"}
Dim counter As Integer = 0
While counter < 3
makeItem(sep, counter, a(counter))
Wend
End Sub

makeList ". "
Print
Print "Press any key to quit"
Sleep
</lang>

{{out}}
<pre>
1. first
2. second
3. third
</pre>


=={{header|Free Pascal}}==
=={{header|Free Pascal}}==
Line 589: Line 546:
makeItem;
makeItem;
end;</lang>
end;</lang>

=={{header|FreeBASIC}}==

FreeBASIC does not currently support either nested procedures or lambda expressions.
The best we can do here is to create two separate procedures but pass the state of the first procedure
by reference to the second procedure so it can be modified by the latter.

<lang freebasic>' FB 1.05.0 Win64

Sub makeItem(sep As String, ByRef counter As Integer, text As String)
counter += 1
Print counter; sep; text
End Sub

Sub makeList(sep As String)
Dim a(0 To 2) As String = {"first", "second", "third"}
Dim counter As Integer = 0
While counter < 3
makeItem(sep, counter, a(counter))
Wend
End Sub

makeList ". "
Print
Print "Press any key to quit"
Sleep
</lang>

{{out}}
<pre>
1. first
2. second
3. third
</pre>

=={{header|Fōrmulæ}}==

In [http://wiki.formulae.org/Nested_function this] page you can see the solution of this task.

Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.

The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.


=={{header|Go}}==
=={{header|Go}}==
Line 1,013: Line 1,012:


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

=={{header|Perl 6}}==

<lang perl6>sub make-List ($separator = ') '){
my $count = 1;

sub make-Item ($item) { "{$count++}$separator$item" }

join "\n", <first second third>».&make-Item;
}

put make-List('. ');</lang>
{{out}}
<pre>1. first
2. second
3. third</pre>


=={{header|Phix}}==
=={{header|Phix}}==
Line 1,219: Line 1,202:
(display (make-list ". "))</lang>
(display (make-list ". "))</lang>


{{out}}
<pre>1. first
2. second
3. third</pre>

=={{header|Raku}}==
(formerly Perl 6)

<lang perl6>sub make-List ($separator = ') '){
my $count = 1;

sub make-Item ($item) { "{$count++}$separator$item" }

join "\n", <first second third>».&make-Item;
}

put make-List('. ');</lang>
{{out}}
{{out}}
<pre>1. first
<pre>1. first