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

Content added Content deleted
Line 293: Line 293:
=={{header|ATS}}==
=={{header|ATS}}==


I am not certain "lazy evaluation" is the best way to describe what is needed, for it implies features of a programming language. Haskell, for example, evaluates lists lazily. ATS2 has the notation '''$delay''' for lazy evaluation of that kind. I considered using such methods, but decided against doing so.
I found the reference to "lazy evaluation" misleading, for it implies a special feature of a programming language. Haskell, for example, evaluates lists "lazily". ATS2 has the notation '''$delay''', and there are various "lazy list" implementations for Scheme. I considered using such methods, but decided against doing so.


What one is--I am certain--supposed to write is means for generating an arbitrary number of terms of a continued fraction, one term after another. It happens that, for a rational number, eventually all further terms are known to be infinite, and so need not be computed. The terms ''could'' be returned as a finite-length list. However, this will not be so when irrational numbers enter the picture. So one needs a way to generate ''any'' number of terms. And this is something that requires no "lazy" features of a language. It could be done about as easily in C as in ATS.
What one is--I am certain--supposed to write is means for generating an arbitrary number of terms of a continued fraction, one term after another. It happens that, for a rational number, eventually all further terms are known to be swamped by an infinity, and so need not be computed. The finite terms ''could'' be returned as a finite-length list. However, this will not be so when irrational numbers enter the picture. Therefore one needs a way to generate ''an indefinite number'' of terms. But this is something that requires no "lazy" features of a language. It could be done easily in standard C! The resulting code might, indeed, evaluate terms "lazily", but no special language features are required.

So I do not use '''$delay''' at all. I do use closures, which standard C does not have, but pairing a regular procedure with an environment could achieve the same effect in C.

<hr/>


In the first example solution, I demonstrate concretely that the method of integer division matters. I use 'Euclidean division' (see ACM Transactions on Programming Languages and Systems, Volume 14, Issue 2, pp 127–144. https://doi.org/10.1145/128861.128862) and show that you get a different continued fraction if you start with (-151)/77 than if you start with 151/(-77). I verified that both continued fractions do equal -(151/77).
In the first example solution, I demonstrate concretely that the method of integer division matters. I use 'Euclidean division' (see ACM Transactions on Programming Languages and Systems, Volume 14, Issue 2, pp 127–144. https://doi.org/10.1145/128861.128862) and show that you get a different continued fraction if you start with (-151)/77 than if you start with 151/(-77). I verified that both continued fractions do equal -(151/77).