4

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.

jdaw1
  • 499
  • 2
  • 9

1 Answers1

5

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

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

True

eldo
  • 67,911
  • 5
  • 60
  • 168
  • 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. – march Dec 20 '15 at 19:26
  • 1
    Read it as Not[this Or that] which must evaluate to True because 9 is positive and Times doesn't apply. – eldo Dec 20 '15 at 19:31
  • Oh right, because the Alternatives is inside the Except! I didn't notice that. Nevermind! – march Dec 20 '15 at 19:33
  • 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.

    – jdaw1 Dec 20 '15 at 21:04