# Compound Statements and returning earlier results in () parentheses

I am trying to debug some script and need to Print[] out some results, sometimes before, and sometimes after, certain executions.

These 3 trials exemplify my question, and the last one is where my problem is -- How do I print out previously calculated results with in parenthesis? (e.g. "previous result is: ..." should be visible)

In:= ClearAll[f, x];
f[x_] := f[x] = Which[
x == 0, 1,
x == 1, (Print["at x=1 "]; 1),
x >= 1, (x*f[x - 1]; Print["main one..."])
]

In:= f

During evaluation of In:= at x=1

During evaluation of In:= main one...

In:= ClearAll[g, x];
g[x_] := g[x] = Which[
x == 0, 1,
x == 1, (Print["at x=1 "]; 1),
x >= 1, (x*g[x - 1]; Print["main one..."])
]

In:= g

During evaluation of In:= at x=1

During evaluation of In:= main one...

In:= ClearAll[h, x];
h[x_] := h[x] = Which[
x == 0, 1,
x == 1, (Print["at x=1 "]; 1),
x >= 1, (Return[x*h[x - 1]]; Print["previous result is: ..."])
]

In:= h

During evaluation of In:= at x=1

Out= 6


This http://reference.wolfram.com/language/ref/CompoundExpression.html?view=all wasn't too helpful, except I think it returns only the last expression in a pair of (). These examples only show the Print[] operation before the desired return value... Create recursive sequence of functions with memoization

UPDATE

Maybe I should clarify.

I want this:

ClearAll[h, x];
h[x_] := h[x] = Which[
x == 0, 1,
x == 1, (Print["at x=1 "]; 1),
x >= 1, (x*h[x - 1]; Print["leaving"])
]

h

at x=1

leaving

leaving

leaving


but also the result to be used in a further, continuing calculation. This way I know it has successfully left this case of Which[].

• Aug 20 '18 at 13:54
• Your use of Return prevents the code after Return from being executed. Aug 20 '18 at 14:31
• in the last one above, without the return, it will only print out 'previous result is:...'. How do I get the result (e.g. 24 for f and a message at the end?
– nate
Aug 20 '18 at 14:33

To perform some actions after computing the result but before returning it, you can use With to store the result temporarily:

ClearAll[h, x];
h[x_] := h[x] = Which[
x == 0, 1,
x == 1, (Print["at x=1 "]; 1),
x >= 1, With[{res=x*h[x - 1]}, Print["leaving"]; res]
]

h
(* at x=1 *)
(* leaving *)
(* leaving *)
(* leaving *)
(* 24 *)


For this particular example, you can further use the following, more Mathematica-like approach:

ClearAll[h, x];
h = 1;
h := (Print["at x=1"]; 1)
h[x_] := h[x] = With[{res=x*h[x - 1]}, Print["leaving"]; res]


An alternative to With and @CarlWoll's answer might also be to (ab)use EchoFunction:

ClearAll[h, x];
h = 1;
h := (Print["at x=1"]; 1)
h[x_] := h[x] = EchoFunction["leaving" &][x*h[x - 1]]


You could define a helper function:

myprint[control_, x_]:=(Print[StringForm[control, x]]; x)


Then:

ClearAll[h, x];
h[x_] := h[x] = Which[
x == 0, 1,
x == 1, myprint["at x=1", x],
x >= 1, myprint["previous result is: 1", x*h[x - 1]]
]

h


at x=1

previous result is: 2

previous result is: 6

previous result is: 24

24

• An alternative to a custom helper function might be to use EchoFunction[StringTemplate["previous result is: 1"]][x * h[x - 1]] Aug 20 '18 at 14:52