The Math Forum

Search All of the Math Forum:

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

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

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

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

Advanced Search

Back to Topic List Back to Topic List  
Frans Martens

Posts: 14
Registered: 12/7/04
Re: [mg4898] Transformation rule exercise
Posted: Oct 9, 1996 2:10 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic 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 &
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! | 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

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

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