Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Re: [mg4898] Transformation rule exercise
Replies: 0

 Search Thread: Advanced Search

 Frans Martens Posts: 14 Registered: 12/7/04
Re: [mg4898] Transformation rule exercise
Posted: Oct 9, 1996 2:10 PM
 Plain Text Reply

Robert Hall wrote

Dear Abbey,

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]

returns

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!
rhall2@gl.umbc.edu | 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

© The Math Forum at NCTM 1994-2018. All Rights Reserved.