Repeat: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Tcl: Added implementation)
m (whitespace/formatting)
Line 3: Line 3:


=={{header|C}}==
=={{header|C}}==
<lang C>
<lang c>#include <stdio.h>
#include <stdio.h>


void repeat(void (*f)(void), int n) {
void repeat(void (*f)(void), int n) {
Line 17: Line 16:
void main(char *argv[], int argc) {
void main(char *argv[], int argc) {
repeat(example, 4);
repeat(example, 4);
}</lang>
}
</lang>


=={{header|D}}==
=={{header|D}}==
Line 40: Line 38:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang Haskell>
<lang Haskell>rep :: Monad m => m () -> Integer -> m ()
rep :: Monad m => m () -> Integer -> m ()
rep _ 0 = return ()
rep _ 0 = return ()
rep f n = f >> rep f (n-1)
rep f n = f >> rep f (n-1)
Line 48: Line 45:
sampleFunction = putStrLn "a"
sampleFunction = putStrLn "a"


main = rep sampleFunction 5
main = rep sampleFunction 5</lang>
</lang>


=={{header|Perl 6}}==
=={{header|Perl 6}}==
Line 77: Line 73:


=={{header|Python}}==
=={{header|Python}}==
<lang Python>
<lang Python>#!/usr/bin/python
#!/usr/bin/python
def repeat(f,n):
def repeat(f,n):
for i in range(n):
for i in range(n):
Line 86: Line 81:
print("Example");
print("Example");


repeat(procedure,3); #prints "Example" (without quotes) three times, separated by newlines.
repeat(procedure,3); #prints "Example" (without quotes) three times, separated by newlines.</lang>
</lang>


=={{header|Racket}}==
=={{header|Racket}}==
The racket guide has a section called [http://docs.racket-lang.org/guide/for.html?q=iterators "Iterators and Comprehensions"], which shows that ''for''
The racket guide has a section called [http://docs.racket-lang.org/guide/for.html?q=iterators "Iterators and Comprehensions"], which shows that ''for'' isn't just for repeating n times!
isn't just for repeating n times!


<lang Racket>#lang racket/base
<lang Racket>#lang racket/base

Revision as of 16:50, 19 May 2014

Repeat 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.

The task is to write a procedure which accepts as arguments another procedure and a positive integer. The latter procedure is executed a number of times equal to the accepted integer.

C

<lang c>#include <stdio.h>

void repeat(void (*f)(void), int n) {

for(int i=n; 0<i; i--)
(*f)();

}

void example() {

printf("Example\n");

}

void main(char *argv[], int argc) {

repeat(example, 4);

}</lang>

D

<lang d>void repeat(void function() fun, in uint times) {

   foreach (immutable _; 0 .. times)
       fun();

}

void procedure() {

   import std.stdio;
   "Example".writeln;

}

void main() {

   repeat(&procedure, 3);

}</lang>

Output:
Example
Example
Example

Haskell

<lang Haskell>rep :: Monad m => m () -> Integer -> m () rep _ 0 = return () rep f n = f >> rep f (n-1)

sampleFunction :: IO () sampleFunction = putStrLn "a"

main = rep sampleFunction 5</lang>

Perl 6

Perl 6 has a built in infix repeat operator (xx) that does this. It returns a list of whatever is on the left side (string, function, subroutine, whatever) times whatever integer is on the right. Nominally it is for building lists but can be used for side effects too. It could be argued that this is an operator, not a procedure, but in Perl 6, operators are just subroutines with funny calling conventions.

Print a list consisting of repeated strings. <lang Perl6>say <Again> xx 4;</lang>

Output:
   Again Again Again Again

Use it for side effects. Execute the code inside the parenthesis repeatedly. <lang Perl6>(say <Again>) xx 3;</lang>

Output:
   Again
   Again
   Again

Print a list built by executing the built in rand function repeatedly. <lang Perl6>say rand xx 5;</lang>

Output:
   0.313024826146821 0.231583221230768 0.439756228609952 0.870180182595826 0.650911888509852

Create a small subroutine, call it repeatedly then print the result. <lang Perl6>sub boguscode () { sleep 1; time; } my @times = boguscode() xx 4; say @times;</lang>

Output:
   1400450920 1400450921 1400450922 1400450923

Python

<lang Python>#!/usr/bin/python def repeat(f,n):

 for i in range(n):
   f();

def procedure():

 print("Example");

repeat(procedure,3); #prints "Example" (without quotes) three times, separated by newlines.</lang>

Racket

The racket guide has a section called "Iterators and Comprehensions", which shows that for isn't just for repeating n times!

<lang Racket>#lang racket/base (define (repeat f n) ; the for loop is idiomatic of (although not exclusive to) racket

 (for ((_ n)) (f)))

(define (repeat2 f n) ; This is a bit more "functional programmingy"

 (when (positive? n) (f) (repeat2 f (sub1 n))))

(display "...") (repeat (λ () (display " and over")) 5) (display "...") (repeat2 (λ () (display " & over")) 5) (newline)</lang>

Output:
... and over and over and over and over and over... & over & over & over & over & over

Tcl

The usual way of doing a repeat would be: <lang tcl>proc repeat {command count} {

   for {set i 0} {$i < $count} {incr i} {
       uplevel 1 $command
   }

}

proc example {} {puts "This is an example"} repeat example 4</lang> However, the time command can be used as long as the return value (the report on the timing information) is ignored. <lang tcl>time example 4</lang> It should be noted that the “command” can be an arbitrary script, not just a call to a procedure: <lang tcl>repeat {puts "hello world"} 3</lang>