This has baffled me for two days now. It's exercise 4 from p. 113 of Introduction to Programming with Mathematica, by Gaylord, Kamin & Wellin. The problem is to rewrite g[x_] = x /. Plus[z___] -> Times[z] so that
g[a + b + c]
a b c The authors provide the following information: "Hint: You need to maintain the lhs of the transformation rule unevaluated for purposes of pattern-matching and the rhs of the rule unevaluated until the rule is used."
Unfortunately, both my solutions violate both clauses of the hint. g1[x_] := x /. Plus -> Times g2[x_] := x /. Plus[y___, z___] -> Times[y, z] I could replace Rule with RuleDelayed in either function (replace -> with :>) but it isn't necessary. The authors
state that the lhs is evaluated before the rule is applied, and
Trace bears this out. I'm looking for a solution to which the authors' hint applies, but for the life of me I can't find it. Am I overlooking something obvious?
Baffled near Baltimore
Bob Hall | "Know thyself? Absurd direction! firstname.lastname@example.org | Bubbles bear no introspection." -Khushhal Khan Khatak
A solution is
g[x_] := (x /. Literal[Plus[z___]] :> Times[z])
Frans Martens Eindhoven University of Technology The Netherlands