4
$\begingroup$

There are a lot of discussions about looping constructs (notable example here). Wolfram language guides, as usually, contain most relevant links.

Looping constructs guide has the following part on the screen.

enter image description here

The link ReplaceRepeated has its place of honor. Nonetheless, if you follow the link "Iterating over lists and expressions >>", ReplaceRepeated is absent. Its own page is rather laconic and has no reference to looping.

As a typical case, I have not noticed Rule-base sample among 'Multiparadigm Language' examples of the compounding task on the ads page. You may see that for 'functional paradigm' stands some:

NestList[# (1 + r) &, s, 3]

{s, (1 + r) s, (1 + r)^2 s, (1 + r)^3 s}

I could imagine something for this compounding task:

ReplaceRepeated[s, s -> s (1 + r), MaxIterations -> 3]

but it gives a warning message (by the way, how to apply a shortcut //. to assume Option MaxIteration?).

ReplaceRepeated::rrlim: Exiting after s scanned 3 times. >>>

(1 + r)^3 s

So, my question is twofold. How to efficienly apply ReplaceRepeated for looping and what are textbooks examples of 'Rule-based programming style' (or Transformation Rules) applications for looping. After all, what is the equivalent for NestList with ReplaceRepeated?

$\endgroup$
  • $\begingroup$ In that page it says "supporting many programming paradigms, such as procedural, functional, rule-based, pattern-based, object-oriented, and more." $\endgroup$ – Dr. belisarius Dec 3 '15 at 17:34
  • $\begingroup$ As for "efficiency", you may find lot of questions on this site about efficiency and rules. It mainly depends on how you use blank patterns $\endgroup$ – Dr. belisarius Dec 3 '15 at 17:36
  • $\begingroup$ @belisariushassettled, i.e. i.e. how blank patterns mimic the structure of compounding $\endgroup$ – garej Dec 3 '15 at 20:11
  • $\begingroup$ Seems like your question is overly too broad $\endgroup$ – Dr. belisarius Dec 3 '15 at 20:12
  • 1
    $\begingroup$ Well, it is a ReplaceAll with FixedPoint. And FixedPoint is an iterative construct ... $\endgroup$ – Dr. belisarius Dec 3 '15 at 20:58
7
$\begingroup$

Using the shortcut //. together with a MaxIterations options

s //. Sequence[s -> s (1 + r), MaxIterations -> 3]

$\ $(1 + r)^3 s

or

s //. (s -> s (1 + r)) ~ Sequence ~ (MaxIterations -> 3)

An example for an efficient looping construct using ReplaceRepeated is the pattern matching Fibonacci sequence generator

fiboSequence2[n_] := 
 Quiet@ReplaceRepeated[{0, 1}, {x___, a_, b_} :> {x, a, b, a + b}, MaxIterations -> n - 1]

fiboSequence2[15]

$\ ${0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610}


The output of

NestList[# (1 + r) &, s, 3]

can be reproduced with

ReplaceRepeated[{s}, 
  {most___, rest_} :> {most, rest, rest /. s -> s (1 + r)}, MaxIterations -> 3]

$\ ${s, (1 + r) s, (1 + r)^2 s, (1 + r)^3 s}

or

{s} //. {most___, rest_} /; Length[{most}] < 3 :> {most, rest, rest (1 + r)}

$\ ${s, (1 + r) s, (1 + r)^2 s, (1 + r)^3 s}

| improve this answer | |
$\endgroup$
3
$\begingroup$

Check out David Wagner's Power Programming with Mathematica, chapter6, Ruled-based programming. (Refer to this question to obtain the download link.)

From Section 6.2.6 Pure ruled-based programming, the author shares the following example using ReplaceRepeated to define the Factorial function on the fly.

f[5] //. {f[0]:>1,f[n_]:>n*f[n-1]}

For the Compounded interest rate, using the method above, the definition would be as follows.

cI[interest_, 
  periods_] := (1 + interest)^
   periods //. (1 + interest) ^
    periods :> (1 + interest) (1 + interest)^(periods - 1)

cI[0.1,12]
(*3.14*)

For the Fibonacci function:

fibo[12] //. {fibo[0] :> 0, fibo[1] :> 1, 
  fibo[n_] :> fibo[n - 1] + fibo[n - 2]}
(*144*)
| improve this answer | |
$\endgroup$
  • $\begingroup$ @Karsten7, thanks, it is the factorial function. $\endgroup$ – Zviovich Dec 3 '15 at 23:43
  • $\begingroup$ With respect to efficiency your on the fly Fibonacci function is more a how not to use ReplaceRepeated example. There are many more efficient ways to program a on the fly Fibonacci function using ReplaceRepeated. $\endgroup$ – Karsten 7. Dec 4 '15 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.