Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Defining a function P[A_ | B_]:=f[A,B] overriding Alternatives

  • To: mathgroup at
  • Subject: [mg19221] Re: Defining a function P[A_ | B_]:=f[A,B] overriding Alternatives
  • From: Eckhard Hennig <hennig at>
  • Date: Wed, 11 Aug 1999 02:06:42 -0400
  • Organization: ITWM
  • References: <7oojvg$>
  • Sender: owner-wri-mathgroup at

Peltio schrieb in Nachricht <7oojvg$i35 at>...
>Is there a general way to call a function with arguments separated not by
>commas but by user defined symbols (particularly Mathematica sacred
>Here's what I mean:
>I was willing to define a function for the calculation of the conditional
>probabilty, via the Bayes formula, and I wanted to define it in such a way
>that the function call would be:
>but Mathematica correctly interpreted the lhs of the definition
>p[A_|B_] as if there were two alternative arguments.
>I can unprotect Alternatives and clear it and that would work,
>p[A_|B_]:= {A,B}
>    {A,B}
>but, is there a less traumatic way to achieve this without fully
>Alternatives? It would be fine to avoid its evaluation only when inside a
>[ ] function, by means of upvalues. But the following does not seems to
>Alternatives /: p[A_|B_]:={A,B}
>And yet I wanted to be able to define my function without showing the
>Alternatives /: part. Is there a way to inhibit the Alternatives evaluation
>inside p[] in a single statement (that could be hidden in a initialization

After defining p[A_ | B_] you can manipulate the DownValues of p as follows
to prevent interpretation of the Alternatives pattern.

p[A_ | B_] := {A, B}

DownValues[p] = DownValues[p] /.
  Alternatives -> HoldPattern[Alternatives];

p[C | D]

{C, D}

However, this requires applying the replacement rule from In[2] after
defining the pattern p[A_|B_]. The following piece of code performs this
replacement automatically; it can be put into an initialization cell. Note
that the SetDelayed pattern only works for the symbol p. The variable
SetDelayedFlag prevents infinite recursion.

SetDelayedFlag = True;
SetDelayed[x_p, y_] /; SetDelayedFlag :=
  Block[{SetDelayedFlag = False},
    x := y;
    DownValues[p] = DownValues[p] /.
      {HoldPattern[Alternatives] -> HoldPattern[Alternatives],
       Alternatives -> HoldPattern[Alternatives]};

p[ A_ | B_] := {A, B}

(* No explicit manipulation of DownValues necessary here. *)

p[C | D]

{C, D}

-- Eckhard

Dipl.-Ing. Eckhard Hennig      mailto:hennig at
Institut fuer Techno- und Wirtschaftsmathematik e.V. (ITWM)
Erwin-Schroedinger-Strasse,  67663 Kaiserslautern,  Germany
  Voice: +49-(0)631-205-3126    Fax: +49-(0)631-205-4139

     ITWM - Makers of Analog Insydes for Mathematica

  • Prev by Date: Re: How do I make my notebook act like ...
  • Next by Date: Re: How to suppress the beep in version 4?
  • Previous by thread: Re: Defining a function P[A_ | B_]:=f[A,B] overriding Alternatives
  • Next by thread: Re: Defining a function P[A_ | B_]:=f[A,B] overriding Alternatives