4

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?

garej
  • 4,865
  • 2
  • 19
  • 42
  • In that page it says "supporting many programming paradigms, such as procedural, functional, rule-based, pattern-based, object-oriented, and more." – Dr. belisarius Dec 03 '15 at 17:34
  • 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 – Dr. belisarius Dec 03 '15 at 17:36
  • @belisariushassettled, i.e. i.e. how blank patterns mimic the structure of compounding – garej Dec 03 '15 at 20:11
  • Seems like your question is overly too broad – Dr. belisarius Dec 03 '15 at 20:12
  • @belisariushassettled, you are authorised to decide. But saying "supporting many programming paradigms, such as procedural, functional, rule-based, pattern-based, object-oriented, and more." they provide all but no rule-based, pattern-based examples. – garej Dec 03 '15 at 20:18
  • @belisariushassettled, to keep it simple, how to get '{s(1+r), s(1+r)^2, s(1+r)^3...}' with blank patterns or whatever that is 'rule-based approach' – garej Dec 03 '15 at 20:20
  • You'll need to specify a stopping condition, like MaxIterations or manually k = s; c = 0; k //. s :> (c++; s (1 + r)) /; c < 3 – Dr. belisarius Dec 03 '15 at 20:37
  • @belisariushassettled, MaxIterations gives me warining, ReplaceRepeated::rrlim: Exiting after s scanned 3 times. Manual stuff looks not much better than For loop. My example corrodes to Union@Flatten@ Quiet@ReplaceRepeated[s, s -> {s, s (1 + r)}, MaxIterations -> 3]. I'm not sure that its a good candidate for the task. Why is it on the 'Looping construct guide'? – garej Dec 03 '15 at 20:47
  • 1
    Well, it is a ReplaceAll with FixedPoint. And FixedPoint is an iterative construct ... – Dr. belisarius Dec 03 '15 at 20:58
  • @belisariushassettled, if that is all about ReplaceAll, that crutch would suffice: Range[3] /. n_Integer :> s (1 + r)^n. But that is a bit different story. – garej Dec 03 '15 at 22:29
  • @Karsten, am I right to say that Quiet@ is inevitable and my example DeleteDuplicates@ Flatten@Quiet@ ReplaceRepeated[s, s -> {s, s (1 + r)}, MaxIterations -> 3] is a good MMA practice? – garej Dec 03 '15 at 22:34
  • I don't consider that message a "warning message". For me it's an information message, telling me that the rule could be applied more times. Instead of Quiet one could also use Off[ReplaceRepeated::rrlim]. There is also nothing wrong with getting that message printed. – Karsten7 Dec 03 '15 at 22:42
  • I wouldn't consider that example good Mma practice, because other functions might be more suitable and you are using a lot of extra functions to make it work. If I'd have to use ReplaceRepeated to get that output I'd probably use something like ReplaceRepeated[{s}, {most___, rest_} :> {most, rest, rest /. s -> s (1 + r)}, MaxIterations -> 3]. – Karsten7 Dec 03 '15 at 22:53

2 Answers2

7

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}

Karsten7
  • 27,448
  • 5
  • 73
  • 134
3

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*)
Zviovich
  • 9,308
  • 1
  • 30
  • 52
  • @Karsten7, thanks, it is the factorial function. – Zviovich Dec 03 '15 at 23:43
  • 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. – Karsten7 Dec 04 '15 at 10:06