Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
category:
- Scope
- Functions and subroutines
from: http://rosettacode.org/wiki/Nested_function

View file

@ -0,0 +1,19 @@
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.
;Task:
Write a program consisting of two nested functions that prints the following text.
1. first
2. second
3. third
The outer function (called <tt>MakeList</tt> or equivalent) is responsible for creating the list as a whole and is given the separator <tt>". "</tt> 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 <tt>MakeItem</tt> or equivalent) is responsible for creating a list item. It accesses the separator from the outer function and modifies the counter.
;References:
:* [[wp:Nested function|Nested function]]
<br><br>

View file

@ -0,0 +1,11 @@
F makeList(separator)
V counter = 1
F makeItem(item)
-V result = @counter@separatoritem"\n"
@counter++
R result
R makeItem(first)makeItem(second)makeItem(third)
print(makeList(. ))

View file

@ -0,0 +1,50 @@
;program starts here, after loading palettes etc.
MOVE.W #3,D1
MOVE.W #'.',D4
JSR MakeList
jmp * ;halt
MakeList:
MOVE.W #1,D0
loop_MakeList:
MOVE.W D0,-(SP)
JSR PrintHex
MOVE.B D4,D0 ;load separator into D0
JSR PrintChar
MOVE.B #' ',D0
JSR PrintChar
MOVE.W (SP)+,D0
JSR MakeItem
CMP.W D0,D1
BCC loop_MakeList ;back to start
RTS
MakeItem:
MOVE.W D0,D2
SUBQ.W #1,D2
LSL.W #2,D2
LEA PointerToText,A0
MOVE.L (A0,D2),A3
JSR PrintString
JSR NewLine
ADDQ.W #1,D0
RTS
PointerToText:
DC.L FIRST,SECOND,THIRD
FIRST:
DC.B "FIRST",0
EVEN
SECOND:
DC.B "SECOND",0
EVEN
THIRD:
DC.B "THIRD",0
EVEN

View file

@ -0,0 +1,12 @@
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( ". " ) ) )

View file

@ -0,0 +1,25 @@
begin
string(30) procedure makeList ( string(2) value separator ) ;
begin
string(30) listValue;
integer counter;
string(10) procedure makeItem ( string(6) value item
; integer value length
) ;
begin
string(10) listItem;
counter := counter + 1;
listItem( 0 // 1 ) := code( decode( "0" ) + counter );
listItem( 1 // 2 ) := separator;
listItem( 3 // 6 ) := item;
listItem( 3 + length // 1 ) := code( 10 );
listItem
end; % makeItem %
counter := 0;
listValue := makeItem( "first", 5 );
listValue( 9 // 10 ) := makeItem( "second", 6 );
listValue( 19 // 10 ) := makeItem( "third", 5 );
listValue
end; % makeList %
write( makeList( ". " ) )
end.

View file

@ -0,0 +1,37 @@
(* ****** ****** *)
//
#include
"share/atspre_staload.hats"
//
(* ****** ****** *)
fun
MakeList
(
sep: string
) : void = let
//
var count: int = 0
//
val count =
$UNSAFE.cast{ref(int)}(addr@count)
//
fun
MakeItem
(
item: string
) : void = let
val () = !count := !count+1
in
println! (!count, sep, item)
end // end of [MakeItem]
//
in
MakeItem"first"; MakeItem"second"; MakeItem"third"
end // end of [MakeList]
(* ****** ****** *)
implement main0() = { val () = MakeList". " }
(* ****** ****** *)

View file

@ -0,0 +1,24 @@
with Ada.Text_IO;
procedure Nested_Functions is -- 'Nested_Functions' is the name of 'main'
type List is array(Natural range <>) of String(1 .. 10);
function Make_List(Separator: String) return List is
Counter: Natural := 0;
function Make_Item(Item_Name: String) return String is
begin
Counter := Counter + 1; -- local in Make_List, global in Make_Item
return(Natural'Image(Counter) & Separator & Item_Name);
end Make_Item;
begin
return (Make_Item("First "), Make_Item("Second"), Make_Item("Third "));
end Make_List;
begin -- iterate through the result of Make_List
for Item of Make_List(". ") loop
Ada.Text_IO.Put_Line(Item);
end loop;
end Nested_Functions;

View file

@ -0,0 +1,54 @@
--------------------- NESTED FUNCTION --------------------
-- makeList :: String -> String
on makeList(separator)
set counter to 0
-- makeItem :: String -> String
script makeItem
on |λ|(x)
set counter to counter + 1
(counter & separator & x & linefeed) as string
end |λ|
end script
map(makeItem, ["first", "second", "third"]) as string
end makeList
--------------------------- TEST -------------------------
on run
makeList(". ")
end run
-------------------- GENERIC FUNCTIONS -------------------
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map

View file

@ -0,0 +1,12 @@
-- makeList :: String -> String
on makeList(separator)
-- makeItem :: String -> Int -> String
script makeItem
on |λ|(x, i)
(i & separator & x & linefeed) as string
end |λ|
end script
map(makeItem, ["first", "second", "third"]) as string
end makeList

View file

@ -0,0 +1,17 @@
makeList: function [separator][
counter: 1
makeItem: function [item] .export:[counter][
result: ~"|counter||separator||item|"
counter: counter+1
return result
]
@[
makeItem "first"
makeItem "second"
makeItem "third"
]
]
print join.with:"\n" makeList ". "

View file

@ -0,0 +1,5 @@
MakeList {
nl@+10 s𝕩 i0
MakeItem {i+1 (•Fmt i)s𝕩}
(nl)´MakeItem¨"first""second""third"
}

View file

@ -0,0 +1,16 @@
#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";
}

View file

@ -0,0 +1,10 @@
string MakeList(string separator)
{
int counter = 1;
Func<string, string> makeItem = item => counter++ + separator + item + "\n";
return makeItem("first") + makeItem("second") + makeItem("third");
}
Console.WriteLine(MakeList(". "));

View file

@ -0,0 +1,8 @@
string MakeList2(string separator)
{
int counter = 1;
return MakeItem("first") + MakeItem("second") + MakeItem("third");
//using string interpolation
string MakeItem(string item) => $"{counter++}{separator}{item}\n";
}

View file

@ -0,0 +1,37 @@
#include<stdlib.h>
#include<stdio.h>
typedef struct{
char str[30];
}item;
item* makeList(char* separator){
int counter = 0,i;
item* list = (item*)malloc(3*sizeof(item));
item makeItem(){
item holder;
char names[5][10] = {"first","second","third","fourth","fifth"};
sprintf(holder.str,"%d%s%s",++counter,separator,names[counter]);
return holder;
}
for(i=0;i<3;i++)
list[i] = makeItem();
return list;
}
int main()
{
int i;
item* list = makeList(". ");
for(i=0;i<3;i++)
printf("\n%s",list[i].str);
return 0;
}

View file

@ -0,0 +1,8 @@
(defn make-list [separator]
(let [x (atom 0)]
(letfn [(make-item [item] (swap! x inc) (println (format "%s%s%s" @x separator item)))]
(make-item "first")
(make-item "second")
(make-item "third"))))
(make-list ". ")

View file

@ -0,0 +1,10 @@
(defun my-make-list (separator)
(let ((counter 0))
(flet ((make-item (item)
(format nil "~a~a~a~%" (incf counter) separator item)))
(concatenate 'string
(make-item "first")
(make-item "second")
(make-item "third")))))
(format t (my-make-list ". "))

View file

@ -0,0 +1,31 @@
include "cowgol.coh";
include "strings.coh";
sub MakeList(sep: [uint8], buf: [uint8]): (out: [uint8]) is
out := buf; # return begin of buffer for ease of use
var counter: uint32 := 0;
# Add item to string
sub AddStr(str: [uint8]) is
var length := StrLen(str);
MemCopy(str, length, buf);
buf := buf + length;
end sub;
sub MakeItem(item: [uint8]) is
counter := counter + 1;
buf := UIToA(counter, 10, buf);
AddStr(sep);
AddStr(item);
AddStr("\n");
end sub;
MakeItem("first");
MakeItem("second");
MakeItem("third");
[buf] := 0; # terminate string
end sub;
var buffer: uint8[100];
print(MakeList(". ", &buffer as [uint8]));

View file

@ -0,0 +1,15 @@
string makeList(string seperator) {
int counter = 1;
string makeItem(string item) {
import std.conv : to;
return to!string(counter++) ~ seperator ~ item ~ "\n";
}
return makeItem("first") ~ makeItem("second") ~ makeItem("third");
}
void main() {
import std.stdio : writeln;
writeln(makeList(". "));
}

View file

@ -0,0 +1,8 @@
fun makeList = List by text separator
int counter = 0
fun makeItem = text by text item
return ++counter + separator + item
end
return text[makeItem("first"), makeItem("second"), makeItem("third")]
end
for each text item in makeList(". ") do writeLine(item) end

View file

@ -0,0 +1,16 @@
module NestedFunction {
static String makeList(String separator) {
Int counter = 1;
function String(String) makeItem = item -> $"{counter++}{separator}{item}\n";
return makeItem("first")
+ makeItem("second")
+ makeItem("third");
}
void run() {
@Inject Console console;
console.print(makeList(". "));
}
}

View file

@ -0,0 +1,15 @@
import extensions;
MakeList(separator)
{
var counter := 1;
var makeItem := (item){ var retVal := counter.toPrintable() + separator + item + (forward newLine); counter += 1; ^ retVal };
^ makeItem("first") + makeItem("second") + makeItem("third")
}
public program()
{
console.printLine(MakeList(". "))
}

View file

@ -0,0 +1,15 @@
defmodule Nested do
def makeList(separator) do
counter = 1
makeItem = fn {}, item ->
{"#{counter}#{separator}#{item}\n", counter+1}
{result, counter}, item ->
{result <> "#{counter}#{separator}#{item}\n", counter+1}
end
{} |> makeItem.("first") |> makeItem.("second") |> makeItem.("third") |> elem(0)
end
end
IO.write Nested.makeList(". ")

View file

@ -0,0 +1,13 @@
USING: io kernel math math.parser locals qw sequences ;
IN: rosetta-code.nested-functions
:: make-list ( separator -- str )
1 :> counter!
[| item |
counter number>string separator append item append
counter 1 + counter!
] :> make-item
qw{ first second third } [ make-item call ] map "\n" join
;
". " make-list write

View file

@ -0,0 +1,6 @@
FUNCTION F(X)
REAL X
DIST(U,V,W) = X*SQRT(U**2 + V**2 + W**2) !The contained function.
T = EXP(X)
F = T + DIST(T,SIN(X),ATAN(X) + 7) !Invoked...
END

View file

@ -0,0 +1,33 @@
SUBROUTINE POOBAH(TEXT,L,SEP) !I've got a little list!
CHARACTER*(*) TEXT !The supplied scratchpad.
INTEGER L !Its length.
CHARACTER*(*) SEP !The separator to be used.
INTEGER N !A counter.
L = 0 !No text is in place.
N = 0 !No items added.
CALL ADDITEM("first") !Here we go.
CALL ADDITEM("second")
CALL ADDITEM("third")
CONTAINS !Madly, defined after usage.
SUBROUTINE ADDITEM(X) !A contained routine.
CHARACTER*(*) X !The text of the item.
N = N + 1 !Count another item in.
TEXT(L + 1:L + 1) = CHAR(ICHAR("0") + N) !Place the single-digit number.
L = L + 1 !Rather than mess with unknown-length numbers.
LX = LEN(SEP) !Now for the separator.
TEXT(L + 1:L + LX) = SEP !Placed.
L = L + LX !Advance the finger.
LX = LEN(X) !Trailing spaces will be included.
TEXT(L + 1:L + LX) = X !Placed.
L = L + LX !Advance the finger.
L = L + 1 !Finally,
TEXT(L:L) = CHAR(10) !Append an ASCII line feed. Starts a new line.
END SUBROUTINE ADDITEM !That was bitty.
END SUBROUTINE POOBAH !But only had to be written once.
PROGRAM POKE
CHARACTER*666 TEXT !Surely sufficient.
INTEGER L
CALL POOBAH(TEXT,L,". ")
WRITE (6,"(A)") TEXT(1:L)
END

View file

@ -0,0 +1,23 @@
SUBROUTINE POOBAH(TEXT,N,SEP) !I've got a little list!
CHARACTER*(*) TEXT(*) !The supplied scratchpad.
INTEGER N !Entry count.
CHARACTER*(*) SEP !The separator to be used.
N = 0 !No items added.
CALL ADDITEM("first") !Here we go.
CALL ADDITEM("second")
CALL ADDITEM("third")
CONTAINS !Madly, defined after usage.
SUBROUTINE ADDITEM(X) !A contained routine.
CHARACTER*(*) X !The text of the item to add.
N = N + 1 !Count another item in.
WRITE (TEXT(N),1) N,SEP,X !Place the N'th text, suitably decorated..
1 FORMAT (I1,2A) !Allowing only a single digit.
END SUBROUTINE ADDITEM !That was simple.
END SUBROUTINE POOBAH !Still worth a subroutine.
PROGRAM POKE
CHARACTER*28 TEXT(9) !Surely sufficient.
INTEGER N
CALL POOBAH(TEXT,N,". ")
WRITE (6,"(A)") (TEXT(I)(1:LEN_TRIM(TEXT(I))), I = 1,N)
END

View file

@ -0,0 +1,38 @@
// In Pascal, functions always _have_ to return _some_ value,
// but the the task doesnt specify what to return.
// Hence makeList and makeItem became procedures.
procedure makeList(const separator: string);
// The var-section for variables that ought to be accessible
// in the routines body as well as the /nested/ routines
// has to appear /before/ the nested routines definitions.
var
counter: 1..high(integer);
procedure makeItem;
begin
write(counter, separator);
case counter of
1:
begin
write('first');
end;
2:
begin
write('second');
end;
3:
begin
write('third');
end;
end;
writeLn();
counter := counter + 1;
end;
// You can insert another var-section here, but variables declared
// in this block would _not_ be accessible in the /nested/ routine.
begin
counter := 1;
makeItem;
makeItem;
makeItem;
end;

View file

@ -0,0 +1,19 @@
' 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

View file

@ -0,0 +1,18 @@
package main
import "fmt"
func makeList(separator string) string {
counter := 1
makeItem := func(item string) string {
result := fmt.Sprintf("%d%s%s\n", counter, separator, item)
counter += 1
return result
}
return makeItem("first") + makeItem("second") + makeItem("third")
}
func main() {
fmt.Print(makeList(". "))
}

View file

@ -0,0 +1,16 @@
import Control.Monad.ST
import Data.STRef
makeList :: String -> String
makeList separator = concat $ runST $ do
counter <- newSTRef 1
let makeItem item = do
x <- readSTRef counter
let result = show x ++ separator ++ item ++ "\n"
modifySTRef counter (+ 1)
return result
mapM makeItem ["first", "second", "third"]
main :: IO ()
main = putStr $ makeList ". "

View file

@ -0,0 +1,7 @@
makeList :: String -> String
makeList separator =
let makeItem = (<>) . (<> separator) . show
in unlines $ zipWith makeItem [1 ..] ["First", "Second", "Third"]
main :: IO ()
main = putStrLn $ makeList ". "

View file

@ -0,0 +1,7 @@
makeList :: String -> String
makeList separator =
let makeItem = unlines . zipWith ((<>) . (<> separator) . show) [1..]
in makeItem ["First", "Second", "Third"]
main :: IO ()
main = putStrLn $ makeList ". "

View file

@ -0,0 +1,10 @@
makeList := method(separator,
counter := 1
makeItem := method(item,
result := counter .. separator .. item .. "\n"
counter = counter + 1
result
)
makeItem("first") .. makeItem("second") .. makeItem("third")
)
makeList(". ") print

View file

@ -0,0 +1,10 @@
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
)

View file

@ -0,0 +1,4 @@
'. ' MakeList 'first';'second';'third'
1. first
2. second
3. third

View file

@ -0,0 +1,17 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
public class NestedFunctionsDemo {
static String makeList(String separator) {
AtomicInteger counter = new AtomicInteger(1);
Function<String, String> makeItem = item -> counter.getAndIncrement() + separator + item + "\n";
return makeItem.apply("first") + makeItem.apply("second") + makeItem.apply("third");
}
public static void main(String[] args) {
System.out.println(makeList(". "));
}
}

View file

@ -0,0 +1,11 @@
function makeList(separator) {
var counter = 1;
function makeItem(item) {
return counter++ + separator + item + "\n";
}
return makeItem("first") + makeItem("second") + makeItem("third");
}
console.log(makeList(". "));

View file

@ -0,0 +1,12 @@
def makeList(separator):
# input: {text: _, counter: _}
def makeItem(item):
(.counter + 1) as $counter
| .text += "\($counter)\(separator)\(item)\n"
| .counter = $counter;
{text:"", counter:0} | makeItem("first") | makeItem("second") | makeItem("third")
| .text
;
makeList(". ")

View file

@ -0,0 +1,21 @@
/* Nested function, in Jsish */
function makeList(separator) {
var counter = 1;
function makeItem(item) {
return counter++ + separator + item + "\n";
}
return makeItem("first") + makeItem("second") + makeItem("third");
}
;makeList('. ');
/*
=!EXPECTSTART!=
makeList('. ') ==> 1. first
2. second
3. third
=!EXPECTEND!=
*/

View file

@ -0,0 +1,13 @@
function makelist(sep::String)
cnt = 1
function makeitem(item::String)
rst = string(cnt, sep, item, '\n')
cnt += 1
return rst
end
return makeitem("first") * makeitem("second") * makeitem("third")
end
print(makelist(". "))

View file

@ -0,0 +1,14 @@
// version 1.0.6
fun makeList(sep: String): String {
var count = 0
fun makeItem(item: String): String {
count++
return "$count$sep$item\n"
}
return makeItem("first") + makeItem("second") + makeItem("third")
}
fun main(args: Array<String>) {
print(makeList(". "))
}

View file

@ -0,0 +1,18 @@
{def makeList
{def makeItem
{lambda {:s :a :i}
{div}{A.first {A.set! 0 {+ {A.first :a} 1} :a}}:s :i}}
{lambda {:s}
{S.map {{lambda {:s :a :i} {makeItem :s :a :i}}
:s {A.new 0}}
first second third
}}}
-> makeList
{makeList .}
->
1. first
2. second
3. third

View file

@ -0,0 +1,10 @@
function makeList (separator)
local counter = 0
local function makeItem(item)
counter = counter + 1
return counter .. separator .. item .. "\n"
end
return makeItem("first") .. makeItem("second") .. makeItem("third")
end
print(makeList(". "))

View file

@ -0,0 +1,75 @@
Module Checkit {
Make_List(". ")
Sub Make_List(Separator$)
Local Counter=0
Make_Item("First")
Make_Item("Second")
Make_Item("Third")
End Sub
Sub Make_Item(Item_Name$)
Counter++
Print Str$(Counter,"")+Separator$+Item_Name$
End Sub
}
Checkit
Module Make_List {
Global Counter=0, Separator$=Letter$
Make_Item("First")
Make_Item("Second")
Make_Item("Third")
Sub Make_Item(Item_Name$)
Counter++
Print Str$(Counter,"")+Separator$+Item_Name$
End Sub
}
Make_List ". "
Module Make_List1 {
Global Counter=0, Separator$=Letter$
Module Make_Item (Item_Name$) {
Counter++
Print Str$(Counter,"")+Separator$+Item_Name$
}
Make_Item "First"
Make_Item "Second"
Make_Item "Third"
}
Make_List1 ". "
Module Make_List (Separator$) {
Def Counter as Integer
// Need New before Item_Name$, because the scope is the module scope
// the scope defined from the calling method.
// by default a function has a new namespace.
Function Make_Item(New Item_Name$){
Counter++
Print Str$(Counter,"")+Separator$+Item_Name$
}
// Call Local place the module scope to function
// function called like a module
Call Local Make_Item("First")
Call Local Make_Item("Second")
Call Local Make_Item("Third")
Print "Counter=";Counter // 3
}
Make_List ". "
Module Make_List (Separator$) {
Def Counter
// using Module not Function.
Module Make_Item(New Item_Name$){
Counter++
Print Str$(Counter,"")+Separator$+Item_Name$
}
Call Local Make_Item,"First"
Call Local Make_Item,"Second"
Call Local Make_Item,"Third"
Print "Counter=";Counter // 3
}
Make_List ". "

View file

@ -0,0 +1,19 @@
makelist:=proc()
local makeitem,i;
i:=1;
makeitem:=proc(i)
if i=1 then
printf("%a\n", "1. first");
elif i=2 then
printf("%a\n","2. second");
elif i=3 then
printf("%a\n", "3. third");
else
return NULL;
end if;
end proc;
while i<4 do
makeitem(i);
i:=i+1;
end do;
end proc;

View file

@ -0,0 +1,6 @@
makeList[sep_String]:=Block[
{counter=0, makeItem},
makeItem[item_String]:=ToString[++counter]<>sep<>item;
makeItem /@ {"first", "second", "third"}
]
Scan[Print, makeList[". "]]

View file

@ -0,0 +1,12 @@
(
:separator
1 :counter
(
:item
item separator counter string ' append append "" join
counter succ @counter
) :make-item
("first" "second" "third") 'make-item map "\n" join
) :make-list
". " make-list print

View file

@ -0,0 +1,10 @@
makeList = function(sep)
counter = 0
makeItem = function(item)
outer.counter = counter + 1
return counter + sep + item
end function
return [makeItem("first"), makeItem("second"), makeItem("third")]
end function
print makeList(". ")

View file

@ -0,0 +1,13 @@
def makeList(separator)
counter = 1
def makeItem(item)
result = str(counter) + separator + item + "\n"
counter += 1
return result
end
return makeItem("first") + makeItem("second") + makeItem("third")
end
println makeList(". ")

View file

@ -0,0 +1,10 @@
proc makeList(separator: string): string =
var counter = 1
proc makeItem(item: string): string =
result = $counter & separator & item & "\n"
inc counter
makeItem("first") & makeItem("second") & makeItem("third")
echo $makeList(". ")

View file

@ -0,0 +1,13 @@
let make_list separator =
let counter = ref 1 in
let make_item item =
let result = string_of_int !counter ^ separator ^ item ^ "\n" in
incr counter;
result
in
make_item "first" ^ make_item "second" ^ make_item "third"
let () =
print_string (make_list ". ")

View file

@ -0,0 +1,14 @@
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;
}

View file

@ -0,0 +1,13 @@
<?
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(". ");
?>

View file

@ -0,0 +1,10 @@
sub makeList {
my $separator = shift;
my $counter = 1;
sub makeItem { $counter++ . $separator . shift . "\n" }
makeItem("first") . makeItem("second") . makeItem("third")
}
print makeList(". ");

View file

@ -0,0 +1,15 @@
function MakeList(string sep=". ")
function MakeItem(integer env, string sep)
integer counter = getd("counter",env)+1
setd("counter",counter,env)
return sprintf("%d%s%s",{counter,sep,{"first","second","third"}[counter]})
end function
integer counter = new_dict({{"counter",0}})
sequence res = {}
for i=1 to 3 do
res = append(res,MakeItem(counter,sep))
end for
return res
end function
?MakeList()

View file

@ -0,0 +1,17 @@
class counter
public integer count
end class
function MakeList(string sep=". ")
function MakeItem(counter c, string sep)
c.count += 1
return sprintf("%d%s%s",{c.count,sep,{"first","second","third"}[c.count]})
end function
counter c = new()
sequence res = {}
for i=1 to 3 do
res = append(res,MakeItem(c,sep))
end for
return res
end function
?MakeList()

View file

@ -0,0 +1,7 @@
(de makeList (Sep)
(let (Cnt 0 makeItem '((Str) (prinl (inc 'Cnt) Sep Str)))
(makeItem "first")
(makeItem "second")
(makeItem "third") ) )
(makeList ". ")

View file

@ -0,0 +1,12 @@
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(". "))

View file

@ -0,0 +1,7 @@
MakeList <- function(sep)
{
counter <- 0
MakeItem <- function() paste0(counter <<- counter + 1, sep, c("first", "second", "third")[counter])
cat(replicate(3, MakeItem()), sep = "\n")
}
MakeList(". ")

View file

@ -0,0 +1,14 @@
/*REXX program demonstrates that functions can be nested (an outer and inner function).*/
ctr= 0 /*initialize the CTR REXX variable.*/
call MakeList '. ' /*invoke MakeList with the separator.*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
MakeItem: parse arg sep,text; ctr= ctr + 1 /*bump the counter variable. */
say ctr || sep || word($, ctr) /*display three thingys ───► terminal. */
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
MakeList: parse arg sep; $= 'first second third' /*obtain an argument; define a string.*/
do while ctr<3 /*keep truckin' until finished. */
call MakeItem sep, $ /*invoke the MakeItem function. */
end /*while*/
return

View file

@ -0,0 +1,13 @@
#lang racket
(define (make-list separator)
(define counter 1)
(define (make-item item)
(begin0
(format "~a~a~a~%" counter separator item)
(set! counter (add1 counter))))
(apply string-append (map make-item '(first second third))))
(display (make-list ". "))

View file

@ -0,0 +1,9 @@
sub make-List ($separator = ') '){
my $count = 1;
sub make-Item ($item) { "{$count++}$separator$item" }
join "\n", <first second third>».&make-Item;
}
put make-List('. ');

View file

@ -0,0 +1,13 @@
# Project : Nested function
makeList(". ")
func makeitem(sep, counter, text)
see "" + counter + sep + text + nl
func makelist(sep)
a = ["first", "second", "third"]
counter = 0
while counter < 3
counter = counter + 1
makeitem(sep, counter, a[counter])
end

View file

@ -0,0 +1,13 @@
def makeList(separator)
counter = 1
makeItem = lambda {|item|
result = "#{counter}#{separator}#{item}\n"
counter += 1
result
}
makeItem["first"] + makeItem["second"] + makeItem["third"]
end
print makeList(". ")

View file

@ -0,0 +1,17 @@
fn make_list(sep: &str) -> String {
let mut counter = 0;
let mut make_item = |label| {
counter += 1;
format!("{}{}{}", counter, sep, label)
};
format!(
"{}\n{}\n{}",
make_item("First"),
make_item("Second"),
make_item("Third")
)
}
fn main() {
println!("{}", make_list(". "))
}

View file

@ -0,0 +1,11 @@
def main(args: Array[String]) {
val sep: String=". "
var c:Int=1;
def go(s: String):Unit={
println(c+sep+s)
c=c+1
}
go("first")
go("second")
go("third")
}

View file

@ -0,0 +1,11 @@
(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 ". "))

View file

@ -0,0 +1,24 @@
$ include "seed7_05.s7i";
const func string: makeList (in string: separator) is func
result
var string: itemList is "";
local
var integer: counter is 1;
const func string: makeItem (in string: item) is func
result
var string: anItem is "";
begin
anItem := counter <& separator <& item <& "\n";
incr(counter);
end func
begin
itemList := makeItem("first") & makeItem("second") & makeItem("third");
end func;
const proc: main is func
begin
write(makeList(". "));
end func;

View file

@ -0,0 +1,11 @@
func make_list(separator = ') ') {
var count = 1
func make_item(item) {
[count++, separator, item].join
}
<first second third>.map(make_item).join("\n")
}
say make_list('. ')

View file

@ -0,0 +1,57 @@
COMMENT CLASS SIMSET IS SIMULA'S STANDARD LINKED LIST DATA TYPE
CLASS HEAD IS THE LIST ITSELF
CLASS LINK IS THE ELEMENT OF A LIST
PROCEDURE IS THE TERM USED FOR FUNCTIONS IN SIMULA
TEXT IS THE TERM USED FOR STRINGS IN SIMULA ;
SIMSET
BEGIN
LINK CLASS ITEM(TXT); TEXT TXT;;
COMMENT CREATING THE LIST AS A WHOLE WITH THE SEPARATOR ". "
GIVEN AS AN ARGUMENT;
REF(HEAD) PROCEDURE MAKELIST(SEPARATOR); TEXT SEPARATOR;
BEGIN
COMMENT VARIABLE TO KEEP TRACK OF THE ITEM NUMBER ;
INTEGER COUNTER;
COMMENT THIS IS THE NESTED FUNCTION ;
REF(ITEM) PROCEDURE MAKEITEM(TXT); TEXT TXT;
BEGIN
TEXT NUM;
TEXT ITEMTEXT;
COMMENT CONVERT NUMBER TO STRING ;
NUM :- BLANKS(5);
NUM.PUTINT(COUNTER);
COMMENT ACCESS SEPARATOR AND MODIFY COUNTER;
COUNTER := COUNTER + 1;
ITEMTEXT :- NUM & SEPARATOR & TXT;
MAKEITEM :- NEW ITEM(ITEMTEXT);
END MAKEITEM;
REF(HEAD) HD;
HD :- NEW HEAD;
COUNTER := 1;
MAKEITEM("FIRST").INTO(HD);
MAKEITEM("SECOND").INTO(HD);
MAKEITEM("THIRD").INTO(HD);
MAKELIST :- HD;
END MAKELIST;
REF(HEAD) LIST;
REF(ITEM) IT;
LIST :- MAKELIST(". ");
COMMENT NAVIGATE THROUGH THE LIST ;
IT :- LIST.FIRST;
WHILE IT =/= NONE DO
BEGIN
OUTTEXT(IT.TXT);
OUTIMAGE;
IT :- IT.SUC;
END;
END.

View file

@ -0,0 +1,15 @@
fun make_list separator =
let
val counter = ref 1;
fun make_item item =
let
val result = Int.toString (!counter) ^ separator ^ item ^ "\n"
in
counter := !counter + 1;
result
end
in
make_item "first" ^ make_item "second" ^ make_item "third"
end;
print (make_list ". ")

View file

@ -0,0 +1,12 @@
(
f = { |separator|
var count = 0;
var counting = { |name|
count = count + 1;
count.asString ++ separator + name ++ "\n"
};
counting.("first") + counting.("second") + counting.("third")
};
)
f.(".")

View file

@ -0,0 +1,13 @@
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(". "))

View file

@ -0,0 +1,15 @@
#!/usr/bin/env tclsh
proc MakeList separator {
set counter 1
proc MakeItem string {
upvar 1 separator separator counter counter
set res $counter$separator$string\n
incr counter
return $res
}
set res [MakeItem first][MakeItem second][MakeItem third]
rename MakeItem {}
return $res
}
puts [MakeList ". "]

View file

@ -0,0 +1,20 @@
Option Explicit
Private Const Sep As String = ". "
Private Counter As Integer
Sub Main()
Dim L As Variant
Counter = 0
L = MakeList(Array("first", "second", "third"))
Debug.Print L
End Sub
Function MakeList(Datas) As Variant
Dim i As Integer
For i = LBound(Datas) To UBound(Datas)
MakeList = MakeList & MakeItem(Datas(i))
Next i
End Function
Function MakeItem(Item As Variant) As Variant
Counter = Counter + 1
MakeItem = Counter & Sep & Item & vbCrLf
End Function

View file

@ -0,0 +1,14 @@
var makeList = Fn.new { |sep|
var counter = 0
var makeItem = Fn.new { |name|
counter = counter + 1
return "%(counter)%(sep)%(name)"
}
var items = []
for (name in ["first", "second", "third"]) {
items.add(makeItem.call(name))
}
System.print(items.join("\n"))
}
makeList.call(". ")

View file

@ -0,0 +1,16 @@
proc MakeList(Separator);
char Separator;
int Counter;
proc MakeItem;
int Ordinals;
[IntOut(0, Counter);
Text(0, Separator);
Ordinals:= [0, "first", "second", "third"];
Text(0, Ordinals(Counter));
CrLf(0);
];
for Counter:= 1 to 3 do MakeItem; \MakeList procedure
MakeList(". ") \main procedure

View file

@ -0,0 +1,8 @@
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(". "));