How can I execute a sequence of statements interactively?

5

2

I'm fairly new to Mathematica. Suppose I have the following function:

f[x_Integer] := Module[{y, z, r},
    y = 5;
    z = 10;
    r = x + y + z;
    r
];

and I would like to halt the execution after each of the statements y = 5, z = 10 and r = x + y + z, and resume only after I press a key. (In other words, I'm looking for something like Console.ReadLine() in C# or getline() in C++.) Is there an easy way to do this in Mathematica?

Thanks in advance for your help.

Shredderroy

Posted 2013-09-09T03:53:05.823

Reputation: 3 879

I think Input[] does the trick. Is the the idiomatic way of implementing the desired outcome? – Shredderroy – 2013-09-09T04:12:05.433

1

Maybe by using built-in debugger? - see here

– Vitaliy Kaurov – 2013-09-09T04:14:31.393

I will certainly look into that. Thank you very much. – Shredderroy – 2013-09-09T05:58:10.103

2@Shredderroy A natural way is to keep open an extra notebook in which to run the code pieces you want to check. – Alexei Boulbitch – 2013-09-09T07:47:52.300

Answers

4

A fairly crude way to do this is to use e.g. DialogInput or friends, but then you have to intersperse your code with a lot of unnecessary lines:

f[x_Integer] := Module[{y, z, r}, y = 5;
   DialogInput["Proceed with z = 10;\n{y,z,r} = " <> ToString@{y, z, r}];
   z = 10;
   DialogInput["Proceed with r = x + y + z;\n{y,z,r} = " <> ToString@{y, z, r}];
   r = x + y + z;
   DialogInput["Proceed with r\n{y,z,r} = " <> ToString@{y, z, r}];
   r];

f[1]

István Zachar

Posted 2013-09-09T03:53:05.823

Reputation: 44 135

this is nice idea..! – Sejwal – 2013-09-09T09:59:17.503

Thank you very much. I will use this for now, until I am ready to spring for Workbench. – Shredderroy – 2013-09-09T21:51:09.130