Repeat: Difference between revisions

7,320 bytes added ,  25 days ago
m
m (syntax highlighting fixup automation)
 
(14 intermediate revisions by 13 users not shown)
Line 327:
 
=={{header|Applesoft BASIC}}==
https://web.archive.org/web/20190202165511/http://hoop-la.ca/apple2/2016/winterwarmup/#repeat.bas
<syntaxhighlight> 100 FOR I = 768 TO 794
110 READ B: POKE I,B: NEXT
120 DATA165,185,72,165,184,72
130 DATA165,118,72,165,117,72
140 DATA169,176,72,32,123,221
150 DATA32,82,231,32,65,217
160 DATA76,210,215
170 POKE 1014,0: POKE 1015,3
 
200 LET P = 400:N = 4
210 GOSUB 300"REPEAT P N
220 END
 
300 IF N < = 0 THEN RETURN
310 LET N = N - 1
320 & P
330 GOTO 300
 
400 PRINT "EXAMPLE"
410 RETURN
</syntaxhighlight>
 
=={{header|Arturo}}==
Line 423 ⟶ 444:
inside odd 3
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="vb">subroutine proc()
print " Inside loop"
end subroutine
 
subroutine repeat(func, times)
for i = 1 to times
call proc()
next
end subroutine
 
call repeat("proc", 5)
print "Loop Ended</syntaxhighlight>
{{out}}
<pre> Inside loop
Inside loop
Inside loop
Inside loop
Inside loop
Loop Ended</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">10 sub proc()
20 print " Inside loop"
30 end sub
40 sub repeat(func$,times)
50 for i = 1 to times
60 proc()
70 next i
80 end sub
90 repeat("proc",5)
100 print "Loop Ended"
110 end</syntaxhighlight>
{{out}}
<pre>Same as BASIC256 entry.</pre>
 
==={{header|GW-BASIC}}===
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|Just BASIC}}
{{works with|Minimal BASIC}}
{{works with|MSX BASIC|any}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
{{works with|Quite BASIC}}
<syntaxhighlight lang="qbasic">100 let f$ = "proc"
110 let c = 5
120 gosub 170
130 print "Loop Ended"
140 goto 220
150 print " Inside loop"
160 return
170 rem repeat(f$,c)
180 for i = 1 to c
190 gosub 150
200 next i
210 return
220 end</syntaxhighlight>
{{out}}
<pre>Same as BASIC256 entry.</pre>
 
==={{header|Minimal BASIC}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|MSX Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">DECLARE SUB rep (func AS STRING, c AS INTEGER)
DECLARE SUB proc ()
 
CALL rep("proc", 5)
PRINT "Loop Ended"
 
SUB proc
PRINT " Inside loop"
END SUB
 
SUB rep (func AS STRING, c AS INTEGER)
FOR i = 1 TO c
proc
NEXT
END SUB</syntaxhighlight>
{{out}}
<pre>Same as BASIC256 entry.</pre>
 
==={{header|Quite BASIC}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">SUB proc
PRINT " Inside loop"
END SUB
 
SUB rep (func$, c)
FOR i = 1 to c
CALL proc
NEXT i
END SUB
 
CALL rep ("proc", 5)
PRINT "Loop Ended"
END</syntaxhighlight>
{{out}}
<pre>Same as BASIC256 entry.</pre>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Repeat"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
DECLARE FUNCTION Proc ()
DECLARE FUNCTION Repe (func$, c)
 
FUNCTION Entry ()
Repe ("proc", 5)
PRINT "Loop Ended"
 
END FUNCTION
 
FUNCTION Proc ()
PRINT " Inside loop"
END FUNCTION
 
FUNCTION Repe (func$, c)
FOR i = 1 TO c
Proc ()
NEXT i
END FUNCTION
END PROGRAM</syntaxhighlight>
{{out}}
<pre>Same as BASIC256 entry.</pre>
 
=={{header|Batch File}}==
Line 514 ⟶ 673:
{{works with|C++11}}
<syntaxhighlight lang="cpp"> repeat([]{std::cout << "Example\n";}, 4);</syntaxhighlight>
 
=={{header|Chapel}}==
 
The most basic form, with generics.
<syntaxhighlight lang="chapel">
config const n = 5;
 
proc example()
{
writeln("example");
}
 
proc repeat(func, n)
{
for i in 0..#n do func();
}
 
repeat(example, n);
</syntaxhighlight>
 
With argument type hinting.
<syntaxhighlight lang="chapel">
config const n = 5;
 
proc example()
{
writeln("example");
}
 
proc repeat(func : proc(), n : uint)
{
for i in 0..#n do func();
}
 
repeat(example, n);
</syntaxhighlight>
 
Example of passing function which takes arguments. Chapel does not allow functions with generic arguments to be passed. First-class functions are still in development: https://chapel-lang.org/docs/technotes/firstClassProcedures.html
<syntaxhighlight lang="chapel">
config const n = 5;
 
proc example(x : uint)
{
writeln("example ", x);
}
 
proc repeat(func : proc(x : uint), n : uint)
{
for i in 0..#n do func(i);
}
 
repeat(example, n);
</syntaxhighlight>
 
=={{header|Clojure}}==
Line 687 ⟶ 899:
<syntaxhighlight lang="factor">: times ( ... n quot: ( ... -- ... ) -- ... )
[ drop ] prepose each-integer ; inline</syntaxhighlight>
 
=={{header|Fe}}==
<syntaxhighlight lang="clojure">
(= repeat (mac (i n . body)
(list 'do
(list 'let i 0)
(list 'while (list '< i n)
(list '= i (list '+ i 1))
(cons 'do body)))))
 
; print multiplication table
(repeat i 10
(repeat j 10
(print i "x" j "=" (* i j)))
(print))
</syntaxhighlight>
 
=={{header|Forth}}==
Line 742 ⟶ 970:
4 proc called
5 proc called
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
void local fn Example( value as long )
NSLog(@"Example %ld",value)
end fn
 
void local fn DoIt( fnAddress as ptr, count as long )
def fn Repeat( j as long ) using fnAddress
long i
for i = 1 to count
fn Repeat( i )
next
end fn
 
fn DoIt( @fn Example, 3 )
 
HandleEvents
</syntaxhighlight>
 
{{out}}
<pre>
Example 1
Example 2
Example 3
</pre>
 
Line 937 ⟶ 1,194:
 
=={{header|Java}}==
There are two ways to achieve this, one way is through ''reflection''.
<syntaxhighlight lang="java">
import java.lang.reflect.Method;
 
public class Program {
public static void main(String[] args) throws ReflectiveOperationException {
Method method = Program.class.getMethod("printRosettaCode");
repeat(method, 5);
}
 
public static void printRosettaCode() {
System.out.println("Rosetta Code");
}
 
public static void repeat(Method method, int count) throws ReflectiveOperationException {
while (count-- > 0)
method.invoke(null);
}
}</syntaxhighlight>
<pre>
Rosetta Code
Rosetta Code
Rosetta Code
Rosetta Code
Rosetta Code
</pre>
<br />
Or
{{works with|Java|8}}
<syntaxhighlight lang="java">import java.util.function.Consumer;
Line 1,045 ⟶ 1,330:
=={{header|Lean}}==
 
It runs on ===Lean 3.4.2:===
<syntaxhighlight lang="lean">
 
<syntaxhighlight lang="lean">def repeat : ℕ → (ℕ → string) → string
| 0 f := "done"
| (n + 1) f := (f n) ++ (repeat n f)
Line 1,054 ⟶ 1,339:
#eval repeat 5 $ λ b : ℕ , "me "
</syntaxhighlight>
 
{{out}}
<pre>
"me me me me me done"
</pre>
 
===Lean 4===
<syntaxhighlight lang="lean">
def repeatf (f : Nat -> String) (n : Nat) : String :=
match n with
| 0 => "."
| (k + 1) => (f k) ++ (repeatf f k)
 
def example1 : String :=
repeatf (fun (x : Nat) => toString (x) ++ " ") (10)
 
#eval example1
</syntaxhighlight>
 
{{out}}
<pre>
"9 8 7 6 5 4 3 2 1 0 ."
</pre>
 
=={{header|LiveCode}}==
Line 1,083 ⟶ 1,391:
Sure looks like a function in here...
Sure looks like a function in here...</pre>
 
=={{header|Mastermind}}==
Functions are in-lined at compile time in [[Mastermind]], meaning the "repeat" function cannot accept another procedure as an argument. This is due to limitations of the compilation target, which is [[Brainf***]]. Dynamically calling functions would require creating a function runtime.
<syntaxhighlight lang="mastermind">def do_something<number> {
output "Letter: ";
output 'a' + number;
output '\n';
}
 
def repeat<times> {
let i = 0;
drain times into i {
do_something<i>;
}
}
 
let repetitions = 8;
repeat<repetitions>;</syntaxhighlight>
{{out}}
<pre>Letter: a
Letter: b
Letter: c
Letter: d
Letter: e
Letter: f
Letter: g
Letter: h
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 1,701 ⟶ 2,037:
hello
</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Repeat 3 Example> Eggs And <Example>>;
}
 
Repeat {
0 s.F e.X = e.X;
s.N s.F e.X = <Repeat <- s.N 1> s.F <Mu s.F e.X>>;
};
 
Example {
e.X = e.X Spam;
};</syntaxhighlight>
{{out}}
<pre>Spam Spam Spam Eggs And Spam</pre>
 
=={{header|REXX}}==
Line 1,745 ⟶ 2,097:
next
</syntaxhighlight>
 
=={{header|RPL}}==
≪ → func n
≪ 1 n '''START''' func EVAL '''NEXT'''
≫ ≫ '<span style="color:blue>TIMES</span>' STO
≪ " world" "Hello" SWAP + ≫ 3 <span style="color:blue>TIMES</span>
{{out}}
<pre>
3: "Hello world"
2: "Hello world"
1: "Hello world"
</pre>
 
=={{header|Ruby}}==
Line 2,095 ⟶ 2,460:
Example 3</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn repeat(n int, f fn()) {
for _ in 0.. n {
f()
Line 2,117 ⟶ 2,482:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var f = Fn.new { |g, n|
for (i in 1..n) g.call(n)
}
Line 2,224 ⟶ 2,589:
===Using self-modifying code===
This method assumes the routine doesn't use the B register, or restores it before returning. Also, the actual "wrapper" used to repeat the passed function must be in RAM so that its instructions can be modified at runtime.
<syntaxhighlight lang="z80">
 
LD HL,myFunc
LD (repeatproc+1),HL
6

edits