55Functional iteration is an elegant way to represent repeated operations that is used a lot.
66"""
77
8- from mathics .core .atoms import Integer1
98from mathics .core .builtin import Builtin
109from mathics .core .convert .python import from_python
1110from mathics .core .evaluation import Evaluation
1211from mathics .core .expression import Expression
1312from mathics .core .expression_predefined import MATHICS3_INFINITY
1413from mathics .core .symbols import Symbol , SymbolTrue
14+ from mathics .core .systemsymbols import SymbolAll
1515
1616# This tells documentation how to sort this module
1717sort_order = "mathics.builtin.iteratively-applying-functions"
1818
1919
2020class FixedPoint (Builtin ):
2121 """
22+ <url>:WMA link:
23+ https://reference.wolfram.com/language/ref/FixedPoint.html</url>
24+
2225 <dl>
2326 <dt>'FixedPoint[$f$, $expr$]'
2427 <dd>starting with $expr$, iteratively applies $f$ until the result no longer changes.
@@ -84,9 +87,13 @@ def eval(self, f, expr, n, evaluation: Evaluation, options: dict):
8487
8588class FixedPointList (Builtin ):
8689 """
90+ <url>:WMA link:
91+ https://reference.wolfram.com/language/ref/FixedPointList.html</url>
92+
8793 <dl>
8894 <dt>'FixedPointList[$f$, $expr$]'
89- <dd>starting with $expr$, iteratively applies $f$ until the result no longer changes, and returns a list of all intermediate results.
95+ <dd>starting with $expr$, iteratively applies $f$ until the result no longer changes, \
96+ and returns a list of all intermediate results.
9097
9198 <dt>'FixedPointList[$f$, $expr$, $n$]'
9299 <dd>performs at most $n$ iterations.
@@ -100,12 +107,17 @@ class FixedPointList(Builtin):
100107 >> newton[9]
101108 = {1., 5., 3.4, 3.02353, 3.00009, 3., 3., 3.}
102109
103- Plot the "hailstone" sequence of a number:
110+ Compute the <url>:Hailstone Number:
111+ https://mathworld.wolfram.com/HailstoneNumber.html</url>: for 14:
112+
104113 >> collatz[1] := 1;
105114 >> collatz[x_ ? EvenQ] := x / 2;
106115 >> collatz[x_] := 3 x + 1;
107116 >> list = FixedPointList[collatz, 14]
108117 = {14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 1}
118+
119+ Plot this:
120+
109121 >> ListLinePlot[list]
110122 = -Graphics-
111123 """
@@ -143,6 +155,9 @@ def eval(self, f, expr, n, evaluation: Evaluation):
143155
144156class Fold (Builtin ):
145157 """
158+ <url>:WMA link:
159+ https://reference.wolfram.com/language/ref/Fold.html</url>
160+
146161 <dl>
147162 <dt>'Fold[$f$, $x$, $list$]'
148163 <dd>returns the result of iteratively applying the binary
@@ -166,6 +181,9 @@ class Fold(Builtin):
166181
167182class FoldList (Builtin ):
168183 """
184+ <url>:WMA link:
185+ https://reference.wolfram.com/language/ref/FoldList.html</url>
186+
169187 <dl>
170188 <dt>'FoldList[$f$, $x$, $list$]'
171189 <dd>returns a list starting with $x$, where each element is
@@ -190,6 +208,9 @@ class FoldList(Builtin):
190208
191209class Nest (Builtin ):
192210 """
211+ <url>:WMA link:
212+ https://reference.wolfram.com/language/ref/Nest.html</url>
213+
193214 <dl>
194215 <dt>'Nest[$f$, $expr$, $n$]'
195216 <dd>starting with $expr$, iteratively applies $f$ $n$ times and returns the final result.
@@ -217,6 +238,9 @@ def eval(self, f, expr, n, evaluation):
217238
218239class NestList (Builtin ):
219240 """
241+ <url>:WMA link:
242+ https://reference.wolfram.com/language/ref/NestList.html</url>
243+
220244 <dl>
221245 <dt>'NestList[$f$, $expr$, $n$]'
222246 <dd>starting with $expr$, iteratively applies $f$ $n$ times and \
@@ -257,6 +281,9 @@ def eval(self, f, expr, n, evaluation):
257281
258282class NestWhile (Builtin ):
259283 """
284+ <url>:WMA link:
285+ https://reference.wolfram.com/language/ref/NestWhile.html</url>
286+
260287 <dl>
261288 <dt>'NestWhile[$f$, $expr$, $test$]'
262289 <dd>applies a function $f$ repeatedly on an expression $expr$, until \
@@ -302,7 +329,7 @@ def eval(self, f, expr, test, m, evaluation: Evaluation):
302329
303330 results = [expr ]
304331 while True :
305- if m . get_name () == "System`All" :
332+ if m is SymbolAll :
306333 test_elements = results
307334 else :
308335 test_elements = results [- m .value :]
0 commit comments