This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,7 @@
Quite often one needs loops which, in the last iteration, execute only part of the loop body. The goal of this task is to demonstrate the best way to do this.
Write a loop which writes the comma-separated list
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
using separate output statements for the number and the comma from within the body of the loop.
See also: [[Loop/Break]]

View file

@ -0,0 +1,2 @@
---
note: Iteration

View file

@ -0,0 +1,6 @@
(defun print-list (xs)
(progn$ (cw "~x0" (first xs))
(if (endp (rest xs))
(cw (coerce '(#\Newline) 'string))
(progn$ (cw ", ")
(print-list (rest xs))))))

View file

@ -0,0 +1,8 @@
FOR i WHILE
print(whole(i, -2));
# WHILE # i < 10 DO
print(", ")
OD;
print(new line)

View file

@ -0,0 +1,8 @@
FOR i TO 10 DO
print(whole(i, -2));
IF i < 10 THEN
print(", ")
FI
OD;
print(new line)

View file

@ -0,0 +1,8 @@
FOR i DO
print(whole(i, -2));
IF i >= 10 THEN GO TO done FI;
print(", ")
OD;
done:
print(new line)

View file

@ -0,0 +1,2 @@
$ awk 'BEGIN{for(i=1;i<=10;i++){printf i;if(i<10)printf ", "};print}'
1, 2, 3, 4, 5, 6, 7, 8, 9, 10

View file

@ -0,0 +1,14 @@
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
procedure Loop_And_Half is
I : Positive := 1;
begin
loop
Put(Item => I, Width => 1);
exit when I = 10;
Put(", ");
I := I + 1;
end loop;
New_Line;
end Loop_And_Half;

View file

@ -0,0 +1,8 @@
PROC main()
DEF i
FOR i := 1 TO 10
WriteF('\d', i)
EXIT i = 10
WriteF(', ')
ENDFOR
ENDPROC

View file

@ -0,0 +1,7 @@
Loop, 9 ; loop 9 times
{
output .= A_Index ; append the index of the current loop to the output var
If (A_Index <> 9) ; if it isn't the 9th iteration of the loop
output .= ", " ; append ", " to the output var
}
MsgBox, %output%

View file

@ -0,0 +1,7 @@
DIM i AS Integer
FOR i=1 TO 10
PRINT i;
IF i=10 THEN EXIT FOR
PRINT ", ";
NEXT i

View file

@ -0,0 +1,5 @@
10 FOR i=1 TO 10
20 PRINT i;
30 IF i=10 THEN GOTO 50
40 PRINT ", ";
50 NEXT i

View file

@ -0,0 +1,5 @@
FOR i% = 1 TO 10
PRINT ; i% ;
IF i% <> 10 PRINT ", ";
NEXT
PRINT

View file

@ -0,0 +1 @@
1+>::.9`#@_" ,",,

View file

@ -0,0 +1,3 @@
"0" v
1+>::"9"`#v_," ,",,>
>"01",,@

View file

@ -0,0 +1,14 @@
#include <iostream>
int main()
{
for (int i = 1; ; i++)
{
std::cout << i;
if (i == 10)
break;
std::cout << ", ";
}
std::cout << std::endl;
return 0;
}

View file

@ -0,0 +1,11 @@
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 10; i++) {
printf("%d", i);
printf(i == 10 ? "\n" : ", ");
}
return 0;
}

View file

@ -0,0 +1,10 @@
; Functional version
(apply str (interpose ", " (range 1 11)))
; Imperative version
(loop [n 1]
(printf "%d" n)
(if (< n 10)
(do
(print ", ")
(recur (inc n)))))

View file

@ -0,0 +1,13 @@
# Loop plus half. This code shows how to break out of a loop early
# on the last iteration. For the contrived example, there are better
# ways to generate a comma-separated list, of course.
start = 1
end = 10
s = ''
for i in [start..end]
# the top half of the loop executes every time
s += i
break if i == end
# the bottom half of the loop is skipped for the last value
s += ', '
console.log s

View file

@ -0,0 +1,7 @@
<cfloop index = "i" from = "1" to = "10">
#i#
<cfif i EQ 10>
<cfbreak />
</cfif>
,
</cfloop>

View file

@ -0,0 +1,12 @@
<cfscript>
for( i = 1; i <= 10; i++ ) //note: the ++ notation works only on version 8 up, otherwise use i=i+1
{
writeOutput( i );
if( i == 10 )
{
break;
}
writeOutput( ", " );
}
</cfscript>

View file

@ -0,0 +1,3 @@
(loop for i from 1 below 10 do
(princ i) (princ ", ")
finally (princ i))

View file

@ -0,0 +1,4 @@
(loop for i from 1 upto 10 do
(princ i)
(if (= i 10) (return))
(princ ", "))

View file

@ -0,0 +1 @@
(format t "~{~a~^, ~}" (loop for i from 1 to 10 collect i))

View file

@ -0,0 +1,12 @@
import std.stdio;
void main() {
for (int i = 1; ; i++) {
write(i);
if (i >= 10)
break;
write(", ");
}
writeln();
}

View file

@ -0,0 +1,5 @@
import std.stdio, std.range, std.algorithm, std.conv, std.string;
void main() {
iota(1, 11).map!text().join(", ").writeln();
}

View file

@ -0,0 +1,7 @@
var i : Integer;
for i := 1 to 10 do begin
Print(i);
if i < 10 then
Print(', ');
end;

View file

@ -0,0 +1,17 @@
program LoopsNPlusOneHalf;
{$APPTYPE CONSOLE}
var
i: integer;
const
MAXVAL = 10;
begin
for i := 1 to MAXVAL do
begin
Write(i);
if i < MAXVAL then
Write(', ');
end;
Writeln;
end.

View file

@ -0,0 +1,7 @@
var i := 1
while (true) {
print(i)
if (i >= 10) { break }
print(", ")
i += 1
}

View file

@ -0,0 +1,11 @@
var i := 1
__loop(fn {
print(i)
if (i >= 10) {
false
} else {
print(", ")
i += 1
true
}
})

View file

@ -0,0 +1,6 @@
for i = 1 to 10 do
printf(1, "%g", {i})
if i < 10 then
puts(1, ", ")
end if
end for

View file

@ -0,0 +1 @@
1[$9>~][$.", "1+]#.

View file

@ -0,0 +1,5 @@
: print-comma-list ( n -- )
[ [1,b] ] keep '[
[ number>string write ]
[ _ = [ ", " write ] unless ] bi
] each nl ;

View file

@ -0,0 +1,13 @@
class Main
{
public static Void main ()
{
for (Int i := 1; i <= 10; i++)
{
Env.cur.out.writeObj (i)
if (i == 10) break
Env.cur.out.writeChars (", ")
}
Env.cur.out.printLine ("")
}
}

View file

@ -0,0 +1,3 @@
: comma-list ( n -- )
dup 1 ?do i 1 .r ." , " loop
. ;

View file

@ -0,0 +1,6 @@
: comma-list ( n -- )
dup 1+ 1 do
i 1 .r
dup i = if leave then \ or DROP UNLOOP EXIT to exit loop and the function
[char] , emit space
loop drop ;

View file

@ -0,0 +1,6 @@
: comma-list ( n -- )
1
begin dup 1 .r
2dup <>
while ." , " 1+
repeat 2drop ;

View file

@ -0,0 +1,49 @@
C WARNING: This program is not valid ANSI FORTRAN 77 code. It uses
C two nonstandard characters on the lines labelled 5001 and 5002.
C Many F77 compilers should be okay with it, but it is *not*
C standard.
PROGRAM LOOPPLUSONEHALF
INTEGER I, TEN
C I'm setting a parameter to distinguish from the label 10.
PARAMETER (TEN = 10)
DO 10 I = 1, TEN
C Write the number only.
WRITE (*,5001) I
C If we are on the last one, stop here. This will make this test
C every iteration, which can slow your program down a little. If
C you want to speed this up at the cost of your own convenience,
C you could loop only to nine, and handle ten on its own after
C the loop is finished. If you don't care, power to you.
IF (I .EQ. TEN) GOTO 10
C Append a comma to the number.
WRITE (*,5002) ','
10 CONTINUE
C Always finish with a newline. This programmer hates it when a
C program does not end its output with a newline.
WRITE (*,5000) ''
STOP
5000 FORMAT (A)
C Standard FORTRAN 77 is completely incapable of completing a
C WRITE statement without printing a newline. This program would
C be much more difficult (i.e. impossible) to write in the ANSI
C standard, without cheating and saying something like:
C
C WRITE (*,*) '1, 2, 3, 4, 5, 6, 7, 8, 9, 10'
C
C The dollar sign at the end of the format is a nonstandard
C character. It tells the compiler not to print a newline. If you
C are actually using FORTRAN 77, you should figure out what your
C particular compiler accepts. If you are actually using Fortran
C 90 or later, you should replace this line with the commented
C line that follows it.
5001 FORMAT (I3, $)
5002 FORMAT (A, $)
C5001 FORMAT (T3, ADVANCE='NO')
C5001 FORMAT (A, ADVANCE='NO')
END

View file

@ -0,0 +1,8 @@
i = 1
do
write(*, '(I0)', advance='no') i
if ( i == 10 ) exit
write(*, '(A)', advance='no') ', '
i = i + 1
end do
write(*,*)

View file

@ -0,0 +1,8 @@
str = ""
for(i = 1; i <= 10; i += 1)
{
str += string(i)
if(i != 10)
str += ", "
}
show_message(str)

View file

@ -0,0 +1,14 @@
package main
import "fmt"
func main() {
for i := 1; ; i++ {
fmt.Print(i)
if i == 10 {
fmt.Println("")
break
}
fmt.Print(", ")
}
}

View file

@ -0,0 +1,5 @@
for(i in (1..10)) {
print i
if (i == 10) break
print ', '
}

View file

@ -0,0 +1,5 @@
loop :: IO ()
loop = mapM_ action [1 .. 10]
where action n = do
putStr $ show n
putStr $ if n == 10 then "\n" else ", "

View file

@ -0,0 +1,4 @@
DO i = 1, 10
WRITE(APPend) i
IF(i < 10) WRITE(APPend) ", "
ENDDO

View file

@ -0,0 +1 @@
print,indgen(10)+1,format='(10(i,:,","))'

View file

@ -0,0 +1,4 @@
for i=1,10 do begin
print,i,format='($,i)'
if i lt 10 then print,",",format='($,a)'
endfor

View file

@ -0,0 +1,5 @@
for i=1,10 do begin
print,i,format='($,i)'
if i eq 10 then break
print,",",format='($,a)'
end

View file

@ -0,0 +1,5 @@
procedure main()
every writes(i := 1 to 10) do
if i = 10 then break write()
else writes(", ")
end

View file

@ -0,0 +1 @@
every writes(c := "",1 to 10) do c := ","

View file

@ -0,0 +1,14 @@
output=: verb define
buffer=: buffer,y
)
loopy=: verb define
buffer=: ''
for_n. 1+i.10 do.
output ":n
if. n<10 do.
output ', '
end.
end.
smoutput buffer
)

View file

@ -0,0 +1,2 @@
loopy 0
1, 2, 3, 4, 5, 6, 7, 8, 9, 10

View file

@ -0,0 +1,2 @@
;}.,(', ' ; ":)&> 1+i.10
1, 2, 3, 4, 5, 6, 7, 8, 9, 10

View file

@ -0,0 +1,3 @@
commaAnd=: ":&.> ;@,. -@# {. (<;._1 '/ and /') ,~ (<', ') #~ #
commaAnd i.5
0, 1, 2, 3 and 4

View file

@ -0,0 +1,9 @@
public static void main(String[] args) {
for (int i = 1; ; i++) {
System.out.print(i);
if (i == 10)
break;
System.out.print(", ");
}
System.out.println();
}

View file

@ -0,0 +1,14 @@
function loop_plus_half(start, end) {
var str = '',
i;
for (i = start; i <= end; i += 1) {
str += i;
if (i === end) {
break;
}
str += ', ';
}
return str;
}
alert(loop_plus_half(1, 10));

View file

@ -0,0 +1,3 @@
p:{`0:$x} / output
i:1;do[10;p[i];p[:[i<10;", "]];i+:1];p@"\n"
1, 2, 3, 4, 5, 6, 7, 8, 9, 10

View file

@ -0,0 +1,2 @@
10 {p@x;p@:[x<10;", ";"\n"];x+1}\1;
{p@x;p@:[x<10;", ";"\n"];x+1}'1+!10; /variant

View file

@ -0,0 +1,2 @@
: , dup ", " 2 compress "" join ;
1 do dup 10 != if dup , . 1 + else . break then loop

View file

@ -0,0 +1,3 @@
: 2string 2 compress "" join ;
: , dup 10 != if ", " 2string then ;
1 10 "dup , . 1+" times

View file

@ -0,0 +1,6 @@
for i =1 to 10
print i;
if i =10 then exit for
print ", ";
next i
end

View file

@ -0,0 +1,19 @@
Section Header
+ name := LOOP_AND_HALF;
Section Public
- main <- (
+ i : INTEGER;
i := 1;
{
i.print;
i = 10
}.until_do {
", ".print;
i := i + 1;
};
'\n'.print;
);

View file

@ -0,0 +1,6 @@
to comma.list :n
repeat :n-1 [type repcount type "|, |]
print :n
end
comma.list 10

View file

@ -0,0 +1,5 @@
for i = 1, 10 do
io.write(i)
if i == 10 then break end
io.write", "
end

View file

@ -0,0 +1,10 @@
define(`break',
`define(`ulim',llim)')
define(`for',
`ifelse($#,0,``$0'',
`define(`ulim',$3)`'define(`llim',$2)`'ifelse(ifelse($3,`',1,
`eval($2<=$3)'),1,
`pushdef(`$1',$2)$4`'popdef(`$1')$0(`$1',incr($2),ulim,`$4')')')')
for(`x',`1',`',
`x`'ifelse(x,10,`break',`, ')')

View file

@ -0,0 +1 @@
printf('%i, ',1:9); printf('%i\n',10);

View file

@ -0,0 +1,6 @@
for k=1:10,
printf('%i', k);
if k==10, break; end;
printf(', ');
end;
printf('\n');

View file

@ -0,0 +1,6 @@
for i in 1 to 10 do
(
format "%" i
if i == 10 then exit
format "%" ", "
)

View file

@ -0,0 +1,9 @@
LOOPHALF
NEW I
FOR I=1:1:10 DO
.WRITE I
.IF I'=10 WRITE ", "
QUIT
;Alternate
NEW I FOR I=1:1:10 WRITE I WRITE:I'=10 ", "
KILL I QUIT

View file

@ -0,0 +1,11 @@
NEXT=`expr $* + 1`
MAX=10
RES=1
all: 1-n;
$(MAX)-n:
@echo $(RES)
%-n:
@-make -f loop.mk $(NEXT)-n MAX=$(MAX) RES=$(RES),$(NEXT)

View file

@ -0,0 +1,8 @@
i = 1; s = "";
While[True,
s = s <> ToString@i;
If[i == 10, Break[]];
s = s <> ",";
i++;
]
s

View file

@ -0,0 +1,8 @@
last := 10;
string s; s := "";
for i = 1 upto last:
s := s & decimal i;
if i <> last: s := s & ", " fi;
endfor
message s;
end

View file

@ -0,0 +1,15 @@
MODULE Loop EXPORTS Main;
IMPORT IO, Fmt;
VAR i := 1;
BEGIN
LOOP
IO.Put(Fmt.Int(i));
IF i = 10 THEN EXIT; END;
IO.Put(", ");
i := i + 1;
END;
IO.Put("\n");
END Loop.

View file

@ -0,0 +1,7 @@
for ($i = 1; $i <= 11; $i++) {
echo $i;
if ($i == 10)
break;
echo ', ';
}
echo "\n";

View file

@ -0,0 +1,6 @@
foreach my $i (1..10) {
print $i;
last if $i == 10;
print ', ';
}
print "\n";

View file

@ -0,0 +1,4 @@
(for N 10
(prin N)
(T (= N 10))
(prin ", ") )

View file

@ -0,0 +1 @@
print ( ', '.join(str(i+1) for i in range(10)) )

View file

@ -0,0 +1,13 @@
>>> from sys import stdout
>>> write = stdout.write
>>> n, i = 10, 1
>>> while True:
write(i)
i += 1
if i > n:
break
write(', ')
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
>>>

View file

@ -0,0 +1 @@
paste(1:10, collapse=", ")

View file

@ -0,0 +1,10 @@
for(i in 1:10)
{
cat(i)
if(i==10)
{
cat("\n")
break
}
cat(", ")
}

View file

@ -0,0 +1,7 @@
/*REXX program to display: 1,2,3,4,5,6,7,8,9,10 */
do j=1 to 10
call charout ,j /*write the DO loop index (no LF)*/
if j<10 then call charout ,"," /*append a comma for 1-digit nums*/
end /*j*/
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,6 @@
/*REXX program to display: 1,2,3,4,5,6,7,8,9,10 */
do j=1 for 10 /*using FOR is faster than TO.*/
call charout ,j || left(',',j<10) /*show J, maybe append a comma.*/
end /*j*/
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,6 @@
for i in 1..10 do
print i
break if i == 10
print ", "
end
puts

View file

@ -0,0 +1,4 @@
@\>@\>@\>+++++++++<!/+. >-?\# digit and loop test
| | \@@@+@+++++# \>>.<.<</ comma and space
| \@@+@@+++++#
\@@@@=++++#

View file

@ -0,0 +1,6 @@
(call-with-current-continuation
(lambda (esc)
(do ((i 1 (+ 1 i))) (#f)
(display i)
(if (= i 10) (esc (newline)))
(display ", "))))

View file

@ -0,0 +1,7 @@
(let loop ((i 0))
(display i)
(if (= i 10)
(newline)
(begin
(display ", ")
(loop (+ 1 i)))))

View file

@ -0,0 +1,6 @@
for {set i 1; set end 10} true {incr i} {
puts -nonewline $i
if {$i >= $end} break
puts -nonewline ", "
}
puts ""

View file

@ -0,0 +1,9 @@
proc range {from to} {
set result {}
for {set i $from} {$i <= $to} {incr i} {
lappend result $i
}
return $i
}
puts [join [range 1 10] ", "] ;# ==> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10