Jump to content

Loops/Increment loop index within loop body: Difference between revisions

→‎{{header|Tcl}}: + standard ML
(→‎{{header|Tcl}}: + standard ML)
Line 3,860:
</pre>
 
=={{header|Standard ML}}==
<lang Standard ML>
fun until done change dolast x =
if done x
then dolast x
else until done change dolast (change x); (* iteration/generic loop *)
 
 
val isprime = fn n :IntInf.int =>
let
fun butlast (_,t) = t*t > n
fun divide (n,t) = n mod t = 0 orelse t*t > n
fun trymore (n,t) = (n,t + 2)
in
 
n mod 2 <> 0 andalso until divide trymore butlast (n,3)
 
end;
 
val loop = fn () =>
let
fun butthislast (_,p,_) = rev p
fun wegot42 (n,_,_) = n = 43
fun trymore (n,p,i) = if isprime i
then ( n+1, (n,i)::p , i+i )
else ( n , p, i+1)
in
 
until wegot42 trymore butthislast (1,[],42)
 
end ;
 
val printp = fn clist:(int*IntInf.int) list =>
List.app (fn i=>print ((Int.toString (#1 i) )^" : "^ (IntInf.toString (#2 i) )^"\n")) clist ; </lang>
call <pre>
printp (loop ()) ;
1 : 43
2 : 89
3 : 179
4 : 359
5 : 719
6 : 1439
7 : 2879
8 : 5779
9 : 11579
10 : 23159
11 : 46327
12 : 92657
13 : 185323
14 : 370661
15 : 741337
16 : 1482707
17 : 2965421
18 : 5930887
19 : 11861791
20 : 23723597
21 : 47447201
22 : 94894427
23 : 189788857
24 : 379577741
25 : 759155483
26 : 1518310967
27 : 3036621941
28 : 6073243889
29 : 12146487779
30 : 24292975649
31 : 48585951311
32 : 97171902629
33 : 194343805267
34 : 388687610539
35 : 777375221081
36 : 1554750442183
37 : 3109500884389
38 : 6219001768781
39 : 12438003537571
40 : 24876007075181
41 : 49752014150467
42 : 99504028301131</pre>
=={{header|Tcl}}==
Inspired by Java and Kotlin variants.
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.