Re: Logical inconsistency
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1249] Re: Logical inconsistency
- From: rubin at msu.edu (Paul A. Rubin)
- Date: Fri, 26 May 1995 07:00:31 -0400
- Organization: Michigan State University
In article <3q14ct$725 at news0.cybernetics.net>, jorma.virtamo at vtt.fi (Jorma Virtamo) wrote: -> ->The other day I tried to construct a rule that applies ->to patterns of type -> -> test = h[a] + h[1] + h[anything]; -> ->i.e. a sum of functions with head h. The most natural thing ->to do is to define the general pattern by -> -> patt = Plus[__h]; -> ->However, when you check for the match, you find to your surprise: -> ->In[3]:= -> MatchQ[test,patt] ->Out[3]= -> False -> ->This happens because -> ->In[4]:= -> patt ->Out[4]= -> __h -> ->or more fully -> ->In[5]:= -> patt // FullForm ->Out[5]=BlankSequence[h] -> ->Because BlankSequence as the argument of Plus is a ->unary argument, Plus "thinks" to be superfluous, and ->drops out. However, BlankSequence outspokenly stands ->for a *sequence* of arguments. Therefore, Plus should ->make an exception to the rule Plus[expr] := expr. -> ->A workaround in this case seems to be -> ->In[6]:= -> patt=(pl_/; pl==Plus)[__h]; -> ->In[7]:= -> MatchQ[test,patt] ->Out[7]= -> True -> -> ->Has anybody a comment? -> -> ->-- Jorma Virtamo -> -> ->============================================================ ->Jorma Virtamo ->VTT Information Technology / Telecommunications ->P.O. Box 1202, FIN-02044 VTT, Finland ->phone: +358 0 456 5612 fax: +358 0 455 0115 ->email: jorma.virtamo at vtt.fi web: http://www.vtt.fi/tte/ ->============================================================ I think the key is not that Plus[x] == x but that the expression Plus[__h] is being evaluated at all. You want to convey to Mathematica that Plus is part of the pattern; you can do that by wrapping it in Literal[], which prevents evaluation. Just change the pattern creation line to patt = Literal[Plus[__h]]; and you get what you want. Paul ************************************************************************** * Paul A. Rubin Phone: (517) 432-3509 * * Department of Management Fax: (517) 432-1111 * * Eli Broad Graduate School of Management Net: RUBIN at MSU.EDU * * Michigan State University * * East Lansing, MI 48824-1122 (USA) * ************************************************************************** Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. J. W. v. GOETHE