Message Boards Message Boards

0
|
8094 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:
GROUPS:

Why "$RecursionLimit::reclim2" without any recursion in sight...

Posted 9 years ago

I have the following code:

f[a_, b_] := {a, b};

Block[
 {
  n := 1,
  l := {{x, y}}
  },
 Do[
  Print[n];
  l := f[{1/n, 1/(n + 1)}, l]; 
  n := n + 1;
  Print[n],
  5
  ]
 ]

which produces

1

and

$RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of n+1. >>

Actually I want to do more complicated things in f with the elements {1,1/2), {1/2,1/3), {1/3,1/4}... and so on. What i dont undestand is why all of a sudden this one here complains about recursion.

I am using 10.2 on PC / OS/X and it happens in a fresh notebook???

POSTED BY: Thomas Vogler
2 Replies

OK. I found it myself. replacing := with = did the trick. Lesson learned: With Mathematica you have to read evey word of the documentation and think twice about it. Then you understand it halfways. := does lazy evaluation and thus n had no value when computing n+1. Using the = operator, assignment was immediate and everyting went smoothly. To my excuse i can say that Pascal was my first language, where := is the regular assignment operator.

POSTED BY: Thomas Vogler

Thomas try n++; instead of n:=n+1; This yields

1 2 2 3 3 4 4 5 5 6

POSTED BY: David Mackay
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract