Continued fraction/Arithmetic/Construct from rational number: Difference between revisions

m
syntax highlighting fixup automation
(Added a Scheme implementation.)
m (syntax highlighting fixup automation)
Line 37:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F r2cf(=n1, =n2)
[Int] r
L n2 != 0
Line 53:
print(r2cf(141421, 100000))
print(r2cf(1414214, 1000000))
print(r2cf(14142136, 10000000))</langsyntaxhighlight>
 
{{out}}
Line 73:
The continued fraction expansion of -151/77 is sensitive to whether the language modulo operator follows the mathematical definition or the C definition.<br>
Algol 68's MOD operator uses the mathematical definition (rounds towards -infinity), so the results for -157//77 agree with the EDSAC, J and a few other sdamples. Most other samples calculate the remainder using the C definotion.
<langsyntaxhighlight lang="algol68">BEGIN # construct continued fraction representations of rational numbers #
# Translated from the C sample #
# Uses code from the Arithmetic/Rational task #
Line 155:
show r2cf( "Running for pi :", pi )
END
END</langsyntaxhighlight>
{{out}}
<pre>
Line 186:
C does not implement Lazy evaluation and it is this particular feature which is the real challenge of this particular example. It can however be simulated. The following example uses pointers. It seems that the same data is being passed but since the function accepts pointers, the variables are being changed. One other way to simulate laziness would be to use global variables. Then although it would seem that the same values are being passed even as constants, the job is actually getting done. In my view, that would be plain cheating.
 
<syntaxhighlight lang="c">
<lang C>
#include<stdio.h>
 
Line 257:
}
</langsyntaxhighlight>
And the run gives :
<pre>
Line 286:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 331:
}
}
</syntaxhighlight>
</lang>
Output
<pre>
Line 357:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
/* Interface for all Continued Fractions
Nigel Galloway, February 9th., 2013.
Line 388:
const int nextTerm() {if (first) {first = false; return 1;} else return 2;}
const bool moreTerms() {return true;}
};</langsyntaxhighlight>
===Testing===
====1/2 3 23/8 13/11 22/7 -151/77====
<langsyntaxhighlight lang="cpp">int main() {
for(r2cf n(1,2); n.moreTerms(); std::cout << n.nextTerm() << " ");
std::cout << std::endl;
Line 405:
std::cout << std::endl;
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 416:
</pre>
====<math>\sqrt 2</math>====
<langsyntaxhighlight lang="cpp">int main() {
int i = 0;
for(SQRT2 n; i++ < 20; std::cout << n.nextTerm() << " ");
Line 425:
std::cout << std::endl;
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 433:
</pre>
====Real approximations of a rational number====
<langsyntaxhighlight lang="cpp">int main() {
for(r2cf n(31,10); n.moreTerms(); std::cout << n.nextTerm() << " ");
std::cout << std::endl;
Line 451:
std::cout << std::endl;
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 465:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn r2cf [n d]
(if-not (= d 0) (cons (quot n d) (lazy-seq (r2cf d (rem n d))))))
 
Line 491:
(doseq [inputs demo
:let [outputs (r2cf (first inputs) (last inputs))]]
(println inputs ";" outputs))</langsyntaxhighlight>
 
{{out}}
Line 517:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun r2cf (n1 n2)
(lambda ()
(unless (zerop n2)
Line 557:
(31428571 10000000)
(314285714 100000000)
(3141592653589793 1000000000000000)))</langsyntaxhighlight>
 
Output:
Line 583:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.concurrency;
import std.stdio;
 
Line 654:
frac.r2cf.iterate;
}
}</langsyntaxhighlight>
 
{{out}}
Line 683:
Besides the assigned task, this program demonstrates a division subroutine
for 35-bit positive integers, returning quotient and remainder.
<langsyntaxhighlight lang="edsac">
[Continued fractions from rationals.
EDSAC program, Initial Orders 2.]
Line 924:
E 13 Z [define entry point]
P F [acc = 0 on entry]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 939:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let rec r2cf n d =
if d = LanguagePrimitives.GenericZero then []
else let q = n / d in q :: (r2cf d (n - q * d))
Line 957:
printfn "%A" (r2cf 1414214 1000000)
printfn "%A" (r2cf 14142136 10000000)
0</langsyntaxhighlight>
Output
<pre>[0; 2]
Line 972:
[1; 2; 2; 2; 2; 2; 2; 2; 2; 2; 6; 1; 2; 4; 1; 1; 2]</pre>
;A version for larger numerators and denominators.
<langsyntaxhighlight lang="fsharp">
let rec rI2cf n d =
if d = 0I then []
else let q = n / d in (decimal)q :: (rI2cf d (n - q * d))
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
Note that the input values are stored as strings and converted to numbers before being fed to <code>r2cf</code>. This is because ratios automatically reduce themselves to the lowest-terms mixed number, which would make for confusing output in this instance.
<langsyntaxhighlight lang="factor">USING: formatting kernel lists lists.lazy math math.parser qw
sequences ;
IN: rosetta-code.cf-arithmetic
Line 1,012:
each ;
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 1,039:
{{works with|gforth|0.7.3}}
 
<langsyntaxhighlight lang="forth">: r2cf ( num1 den1 -- num2 den2 ) swap over >r s>d r> sm/rem . ;
 
: .r2cf ( num den -- )
Line 1,067:
314285714 100000000 .r2cf
3141592653589793 1000000000000000 .r2cf ;
r2cf-demo</langsyntaxhighlight>
 
{{out}}
Line 1,093:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
'with some other constants
data 1,2, 21,7, 21,-7, 7,21, -7,21
Line 1,151:
sleep
system
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,190:
 
File <code>cf.go</code>:
<langsyntaxhighlight Golang="go">package cf
 
import (
Line 1,264:
}
}
}</langsyntaxhighlight>
File <code>rat.go</code>:
<langsyntaxhighlight Golang="go">package cf
 
import "fmt"
Line 1,296:
// Rosetta Code task explicitly asked for this function,
// so here it is. We'll just use the types above instead.
func r2cf(n1, n2 int64) ContinuedFraction { return Rat{n1, n2}.CFTerms }</langsyntaxhighlight>
File <code>rat_test.go</code>:
<langsyntaxhighlight Golang="go">package cf
 
import (
Line 1,344:
// [… commented output used by go test omitted for
// Rosetta Code listing; it is the same as below …]
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,390:
{{trans|Python}}
This more general version generates a continued fraction from any real number (with rationals as a special case):
<langsyntaxhighlight lang="haskell">import Data.Ratio ((%))
 
real2cf :: (RealFrac a, Integral b) => a -> [b]
Line 1,406:
[ real2cf (13 % 11)
, take 20 $ real2cf (sqrt 2)
]</langsyntaxhighlight>
{{Out}}
<pre>[1,5,2]
Line 1,417:
 
This version is a modification of an explicit version shown in http://www.jsoftware.com/jwiki/Essays/Continued%20Fractions to comply with the task specifications.
<langsyntaxhighlight lang="j">cf=: _1 1 ,@}. (, <.)@%@-/ ::]^:a:@(, <.)@(%&x:/)</langsyntaxhighlight>
==== Examples ====
<langsyntaxhighlight lang="j"> cf each 1 2;3 1;23 8;13 11;22 7;14142136 10000000;_151 77
┌───┬─┬─────┬─────┬───┬─────────────────────────────────┬─────────┐
│0 2│3│2 1 7│1 5 2│3 7│1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2│_2 25 1 2│
Line 1,430:
┌────┬─────┬──────────┬───────┬────────┬──────────┬────────────┬───────────┐
│3 10│3 7 7│3 7 23 1 2│3 7 357│3 7 2857│3 7 142857│3 7 476190 3│3 7 7142857│
└────┴─────┴──────────┴───────┴────────┴──────────┴────────────┴───────────┘</langsyntaxhighlight>
This tacit version first produces the answer with a trailing ∞ (represented by _ in J) which is then removed by the last operation (_1 1 ,@}. ...). A continued fraction can be evaluated using the verb ((+%)/) and both representations produce equal results,
<langsyntaxhighlight lang="j"> 3 7 =&((+ %)/) 3 7 _
1</langsyntaxhighlight>
Incidentally, J and Tcl report a different representation for -151/77 versus the representation of some other implementations; however, both representations produce equal results.
<langsyntaxhighlight lang="j"> _2 25 1 2 =&((+ %)/) _1 _1 _24 _1 _2
1</langsyntaxhighlight>
 
===Tacit version 2===
Line 1,442:
Translation of python
 
<langsyntaxhighlight Jlang="j">r2cf=:1 1{."1@}.({:,(0,{:)#:{.)^:(*@{:)^:a:</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight Jlang="j"> ((":@{.,'/',":@{:),': ',":@r2cf)@>1 2;3 1;23 8;13 11;22 7;14142136 10000000;_151 77;14142 10000;141421 100000;1414214 1000000;14142136 10000000;31 10;314 100;3142 1000;31428 10000;314285 100000;3142857 1000000;31428571 10000000;314285714 100000000
1/2: 0 2
3/1: 3
Line 1,465:
3142857/1000000: 3 7 142857
31428571/10000000: 3 7 476190 3
314285714/100000000: 3 7 7142857 </langsyntaxhighlight>
 
===Explicit versions===
==== version 1 ====
Implemented as a class, r2cf preserves state in a separate locale. I've used some contrivances to jam the examples onto one line.
<syntaxhighlight lang="j">
<lang J>
coclass'cf'
create =: dyad def 'EMPTY [ N =: x , y'
Line 1,503:
│_151 77 │_2 25 1 2 │
└─────────────────┴─────────────────────────────────┘
)</langsyntaxhighlight>
==== version 2 ====
<syntaxhighlight lang="j">
<lang J>
f =: 3 : 0
a =. {.y
Line 1,518:
┌───┬─┬─────┬─────┬───┬───────────────────────────────────┬─────────┐
│0 2│3│2 1 7│1 5 2│3 7│1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2 _│_2 25 1 2│
└───┴─┴─────┴─────┴───┴───────────────────────────────────┴─────────┘</langsyntaxhighlight>
 
==== version 3 ====
Line 1,524:
translation of python:
 
<langsyntaxhighlight Jlang="j">r2cf=:3 :0
'n1 n2'=. y
r=.''
Line 1,531:
r=.r,t1
end.
)</langsyntaxhighlight>
 
Example:
 
<langsyntaxhighlight Jlang="j"> r2cf each 1 2;3 1;23 8;13 11;22 7;14142136 10000000;_151 77
┌───┬─┬─────┬─────┬───┬─────────────────────────────────┬─────────┐
│0 2│3│2 1 7│1 5 2│3 7│1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2│_2 25 1 2│
└───┴─┴─────┴─────┴───┴─────────────────────────────────┴─────────┘</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
{{works with|Java|9}}
<langsyntaxhighlight Javalang="java">import java.util.Iterator;
import java.util.List;
import java.util.Map;
Line 1,619:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 1 / 2 = 0 2
Line 1,648:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia"># It'st most appropriate to define a Julia iterable object for this task
# Julia doesn't have Python'st yield, the closest to it is produce/consume calls with Julia tasks
# but for various reasons they don't work out for this task
Line 1,702:
 
println(collect(ContinuedFraction(13 // 11))) # => [1, 5, 2]
println(collect(ContinuedFraction(√2), 20)) # => [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
// compile with -Xcoroutines=enable flag from command line
 
Line 1,749:
iterate(r2cf(frac))
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,779:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Mathematica has a build-in function ContinuedFraction.
<langsyntaxhighlight lang="mathematica">ContinuedFraction[1/2]
ContinuedFraction[3]
ContinuedFraction[23/8]
Line 1,788:
ContinuedFraction[141421/100000]
ContinuedFraction[1414214/1000000]
ContinuedFraction[14142136/10000000]</langsyntaxhighlight>
{{Out}}
<pre>{0, 2}
Line 1,802:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE ConstructFromrationalNumber;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,877:
 
ReadChar;
END ConstructFromrationalNumber.</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">iterator r2cf*(n1, n2: int): int =
var (n1, n2) = (n1, n2)
while n2 != 0:
Line 1,903:
for pair in [(31,10), (314,100), (3142,1000), (31428,10000), (314285,100000),
(3142857,1000000), (31428571,10000000), (314285714,100000000)]:
echo pair, " -> ", toSeq(r2cf(pair[0], pair[1]))</langsyntaxhighlight>
 
{{out}}
Line 1,928:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">apply(contfrac,[1/2,3,23/8,13/11,22/7,-151/77])</langsyntaxhighlight>
{{out}}
<pre>[[0, 2], [3], [2, 1, 7], [1, 5, 2], [3, 7], [-2, 25, 1, 2]]</pre>
Line 1,934:
=={{header|Perl}}==
To do output one digit at a time, we first turn off buffering to be pedantic, then use a closure that yields one term per call.
<langsyntaxhighlight lang="perl">$|=1;
 
sub rc2f {
Line 1,959:
for ([14142,10000],[141421,100000],[1414214,1000000],[14142136,10000000]);
print "\n";
rcshow(rc2f(314285714,100000000));</langsyntaxhighlight>
{{out}}
<pre>
Line 1,978:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">r2cf</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">num</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">denom</span><span style="color: #0000FF;">)</span>
Line 2,015:
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"for sqrt(2)"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sqrt2</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"for pi"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,046:
=={{header|Python}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="python">def r2cf(n1,n2):
while n2:
n1, (t1, n2) = n2, divmod(n1, n2)
Line 2,059:
print(list(r2cf(141421,100000))) # => [1, 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
print(list(r2cf(1414214,1000000))) # => [1, 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
print(list(r2cf(14142136,10000000))) # => [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]</langsyntaxhighlight>
This version generates it from any real number (with rationals as a special case):
<langsyntaxhighlight lang="python">def real2cf(x):
while True:
t1, f = divmod(x, 1)
Line 2,073:
 
print(list(real2cf(Fraction(13, 11)))) # => [1, 5, 2]
print(list(islice(real2cf(2 ** 0.5), 20))) # => [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]</langsyntaxhighlight>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery">
<lang Quackery>
[ $ "bigrat.qky" loadfile ] now!
 
Line 2,110:
dup echo
say " = "
cf echo cr ]</langsyntaxhighlight>
 
{{out}}
Line 2,135:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 2,158:
(real->cf (sqrt 2) 10)
(real->cf pi 10)
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,171:
(formerly Perl 6)
Straightforward implementation:
<syntaxhighlight lang="raku" perl6line>sub r2cf(Rat $x is copy) {
gather loop {
$x -= take $x.floor;
Line 2,179:
}
 
say r2cf(.Rat) for <1/2 3 23/8 13/11 22/7 1.41 1.4142136>;</langsyntaxhighlight>
{{out}}
<pre>(0 2)
Line 2,189:
(1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2)</pre>
As a silly one-liner:
<syntaxhighlight lang="raku" perl6line>sub r2cf(Rat $x is copy) { gather $x [R/]= 1 while ($x -= take $x.floor) > 0 }</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 2,198:
* &nbsp; Checks were included to verify that the arguments being passed to &nbsp; '''r2cf''' &nbsp; are indeed numeric and also not zero.
* &nbsp; This REXX version also handles negative numbers.
<langsyntaxhighlight lang="rexx">/*REXX program converts a decimal or rational fraction to a continued fraction. */
numeric digits 230 /*determines how many terms to be gened*/
say ' 1/2 ──► CF: ' r2cf( '1/2' )
Line 2,255:
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
numeric digits d; return g/1</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default (internal) inputs:}}
<pre>
Line 2,281:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby"># Generate a continued fraction from a rational number
 
def r2cf(n1,n2)
Line 2,288:
yield t1
end
end</langsyntaxhighlight>
===Testing===
'''Test 1:'''
<langsyntaxhighlight lang="ruby">[[1,2], [3,1], [23,8], [13,11], [22,7], [-151,77]].each do |n1,n2|
print "%10s : " % "#{n1} / #{n2}"
r2cf(n1,n2) {|n| print "#{n} "}
puts
end</langsyntaxhighlight>
{{out}}
<pre>
Line 2,307:
'''Test 2:'''
<math>\sqrt 2</math>
<langsyntaxhighlight lang="ruby">(5..8).each do |digit|
n2 = 10 ** (digit-1)
n1 = (Math.sqrt(2) * n2).round
Line 2,313:
r2cf(n1,n2) {|n| print "#{n} "}
puts
end</langsyntaxhighlight>
{{out}}
<pre>
Line 2,322:
</pre>
'''Test 3:'''
<langsyntaxhighlight lang="ruby">a =[ [31,10],
[314,100],
[3142,1000],
Line 2,335:
r2cf(n1,n2) {|n| print "#{n} "}
puts
end</langsyntaxhighlight>
{{out}}
<pre>
Line 2,349:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
struct R2cf {
n1: i64,
Line 2,404:
printcf!(314_285_714, 100_000_000);
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,430:
{{works with|Chez Scheme}}
'''The Implementation'''
<langsyntaxhighlight lang="scheme">; Create a terminating Continued Fraction generator for the given rational number.
; Returns one term per call; returns #f when no more terms remaining.
(define make-continued-fraction-gen
Line 2,466:
(set! lst (append lst (list term)))
(loop (cf))))
lst)))</langsyntaxhighlight>
'''The Task'''
<br />
Each continued fraction is displayed in both the conventional written form and as a list of terms.
<langsyntaxhighlight lang="scheme">(printf "~%Basic examples:~%")
(for-each
(lambda (rat)
Line 2,490:
(printf "~a : ~a~%" rat (rat->cf-list rat)))
'(31/10 314/100 3142/1000 31428/10000 314285/100000 3142857/1000000
31428571/10000000 314285714/100000000 31415926535898/10000000000000))</langsyntaxhighlight>
{{out}}
<pre>
Line 2,544:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func r2cf(num, den) {
func() {
den || return nil
Line 2,568:
seq.each { |r| showcf(r2cf(r.nude)) }
print "\n"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,590:
{{trans|Ruby}}
===Direct translation===
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
proc r2cf {n1 {n2 1}} {
Line 2,608:
return -code break
}} $n1 $n2
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">proc printcf {name cf} {
puts -nonewline "$name -> "
while 1 {
Line 2,640:
} {
printcf "\[$n1;$n2\]" [r2cf $n1 $n2]
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,664:
</pre>
===Objectified version===
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
# General generator class based on coroutines
Line 2,772:
# Demonstrate parsing of input in forms other than a direct pair of decimals
printcf "1.5" [R2CF new 1.5]
printcf "23/7" [R2CF new 23/7]</langsyntaxhighlight>
{{out}}
<pre>
Line 2,801:
{{libheader|Wren-rat}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/rat" for Rat
import "/fmt" for Fmt
 
Line 2,840:
System.print()
i = i + 1
}</langsyntaxhighlight>
 
{{out}}
Line 2,870:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
real Val;
 
Line 2,894:
RlOut(0, Val); CrLf(0);
I:= I+2];
]</langsyntaxhighlight>
{{out}}
<pre>
Line 2,908:
 
Light weight, explicit state:
<langsyntaxhighlight lang="zkl">fcn r2cf(nom,dnom){ // -->Walker (iterator)
Walker.tweak(fcn(state){
nom,dnom:=state;
Line 2,916:
n
}.fp(List(nom,dnom))) // partial application (light weight closure)
}</langsyntaxhighlight>
Heavy weight, implicit state:
<langsyntaxhighlight lang="zkl">fcn r2cf2(nom,dnom){ // -->Generator (heavy weight Walker)
Utils.Generator(fcn(nom,dnom){
while(dnom){
Line 2,926:
Void.Stop;
},nom,dnom)
}</langsyntaxhighlight>
Both of the above return an iterator so they function the same:
<langsyntaxhighlight lang="zkl">foreach nom,dnom in (T(T(1,2), T(3,1), T(23,8), T(13,11), T(22,7),
T(14142,10000), T(141421,100000), T(1414214,1000000),
T(14142136,10000000))){
r2cf(nom,dnom).walk(25).println(); // print up to 25 numbers
}</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits