# Accessing temporary value of a Nest evaluation

The function Nest[] must store the temporary values in the memory somewhere (those values that would form the entries of the list which is returned by the equivalent NestList function).

Does anyone know if there is a way to access said values in an interrupted evaluation (using a kernel subsession)?

Alternatively, is there a way to know all temporary variables currently in use and to print out their values?

-
So you want to manualy abort Nest when you see that current value is what you want? – Kuba Aug 19 '13 at 9:18
Probably some sort of Reap and Sow combination but some more details would be welcome. – Mike Honeychurch Aug 19 '13 at 9:25
You can assign a value to an arbitrary control variable in the function you are Nesting and display that any way you like... In any case, some code/MWE would be very helpful. – Yves Klett Aug 19 '13 at 9:27
Let's say I started an iterative computation, e.g. an optimization and it takes too long. Instead of waiting until it's finished (if it ever does), I want to check where it currently is. – S.Surace Aug 19 '13 at 11:33

I don't know about temporary values, but if you want to monitor the progression of Nest you could use the following:

intensiveCalculation[x_] := (Pause[0.5]; x + 1)

Nest[(PrintTemporary[#]; #) &@ intensiveCalculation[#] &, 1, 5]

-

If I understand you question correctly, you want to run a Nest command. In cases where the Nest takes a long time you'd like to abort (or suspend) the command and then through an interactive debugging session see the values obtained thus far. If this is what you are asking, to the best of my knowledge this is not possible. You need to add code (like Sow) prior to the execution.

-
Does Reap also work for interrupted evaluations? – Yves Klett Aug 19 '13 at 15:57
@YvesKlett, I have never tried that. – user21 Aug 19 '13 at 16:33
Yes, that's what I want to do! Thanks! – S.Surace Aug 20 '13 at 10:41

An alternative using Dynamic to give you a constantly updated progress indicator:

intensiveCalculation[x_] := (Pause[0.5]; x + 1)

Dynamic[y]

Nest[(y = #; intensiveCalculation[#]) &, 1, 10]

-