Loops/Increment loop index within loop body: Difference between revisions
Content added Content deleted
(Realize in F#) |
|||
Line 435: | Line 435: | ||
// unfold is sort of a loop incremented by 1 in this case |
// unfold is sort of a loop incremented by 1 in this case |
||
let fG n=Seq.unfold(fun n->Some(n,(n+1UL))) n|>Seq.find(fN) |
let fG n=Seq.unfold(fun n->Some(n,(n+1UL))) n|>Seq.find(fN) |
||
// unfold is sort of a loop with fG as an internal loop incremented by |
// unfold is sort of a loop with fG as an internal loop incremented by the exit value of the internal loop in this case. |
||
Seq.unfold(fun n->let n=fG n in Some(n,n+n)) 42UL|>Seq.take 42|>Seq.iteri(fun n g->printfn "%2d -> %s" (n+1) (cUL g)) |
Seq.unfold(fun n->let n=fG n in Some(n,n+n)) 42UL|>Seq.take 42|>Seq.iteri(fun n g->printfn "%2d -> %s" (n+1) (cUL g)) |
||
</lang> |
</lang> |
||
Line 483: | Line 483: | ||
42 -> 99,504,028,301,131 |
42 -> 99,504,028,301,131 |
||
</pre> |
</pre> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Explicit loop indices are non-idiomatic, but Factor is certainly capable of using them. Factor has a for loop near-equivalent, <code><range> [ ] each</code>, but since it doesn't mesh well with mutation, a while loop is used. |
Explicit loop indices are non-idiomatic, but Factor is certainly capable of using them. Factor has a for loop near-equivalent, <code><range> [ ] each</code>, but since it doesn't mesh well with mutation, a while loop is used. |