4
$\begingroup$

A PostScriptForm1 for Mathematica must recurse over the likes of Plus[…]. Output should be as follows:

  • 9+n: either 9 n add or n 9 add
  • 9-n: 9 n sub
  • -9+n: n 9 sub
  • -9-n: -9 n sub

So for my purposes first step is to find the first item of a list that is neither Times[-1, _] nor (n_Integer /; n < 0). But Position[(9 + n), (Except[Times[-1, _]] && Except[(n_Integer /; n < 0)]), 1, Heads -> False] returns a grumble: “Except::named: "Named pattern variables are not allowed in the first argument of Except[n_Integer/;n<0]”.

Please, kind experts of Mathematica.StackExchange.com, how could this most naturally be done?

This problem has raised other issues — likely to be my failure to master Mathematica’s object model.

thing = 9 (* Easy peasy *)
MatchQ[thing, (Except[Times[-1, _]])] (* returns True: happiness *) 
MatchQ[thing, (Except[_?Negative])] (* also returns True: happiness *) 
MatchQ[thing, (Except[Times[-1, _]] && Except[_?Negative])] (* returns False in Mathematica 9.0 (January 24, 2013): why? *) 

Guidance would be most welcome. Thank you.

$\endgroup$
3
  • $\begingroup$ You can't use && because that is a logical connective (i.e. it joins logical statements that will evaluate to True or False), where in MatchQ, you are matching patterns, not finding the expressions where the second argument evaluates to True. $\endgroup$
    – march
    Commented Dec 20, 2015 at 19:21
  • $\begingroup$ Perhaps this question and answers will be helpful. $\endgroup$
    – march
    Commented Dec 20, 2015 at 19:31
  • 2
    $\begingroup$ Not an answer, but just wanted to note that that "named patterns" limitation was addressed somewhere in the version 10 releases. $\endgroup$ Commented Dec 20, 2015 at 20:44

1 Answer 1

5
$\begingroup$

You can combine two or more exceptions with Alternatives (|)

MatchQ[thing, Except[Times[-1, _] | _?Negative]]

True

$\endgroup$
4
  • $\begingroup$ I thought of this, too, but then I realized that Alternatives is more like Or than And, because MatchQ will return True if any of the patterns joined by Alternatives match the expression, rather than all. $\endgroup$
    – march
    Commented Dec 20, 2015 at 19:26
  • 1
    $\begingroup$ Read it as Not[this Or that] which must evaluate to True because 9 is positive and Times doesn't apply. $\endgroup$
    – eldo
    Commented Dec 20, 2015 at 19:31
  • $\begingroup$ Oh right, because the Alternatives is inside the Except! I didn't notice that. Nevermind! $\endgroup$
    – march
    Commented Dec 20, 2015 at 19:33
  • $\begingroup$ Just what was needed: thank you. I might have been confused because Position seems to take either a pattern or a function evaluating to a Boolean. Whatever, problem cured: thank you. $\endgroup$
    – jdaw1
    Commented Dec 20, 2015 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.