While attempting to answer my own question, Is there a way to "hold" prefix / infix / postfix notation?, I came across the need to evaluate a recursive function only one level deep.
For example, consider a factorial function:
factorial[n_Integer /; n >= 0] := If[n >= 2, n*factorial[n - 1], 1]
Normally, expressions using
factorial would be fully evaluated:
2 + 3*(factorial + factorial) (* 15482 *)
However, is there a way to evaluate
factorial just once, and prevent evaluating any further occurrences? That is, a function,
EvaluateHeadOnce[factorial, 2 + 3*(factorial + factorial)] (* 2 + 3 (5 factorial + factorial) *)
I found a method (self-answered below) but it felt rather... inelegant. I'm curious to see how others would implement such a method, as well as hear criticisms / flaws in my approach.