Re: Switch and List: How to combine?
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: Switch and List: How to combine?
- From: HAY at leicester.ac.uk
- Date: Mon, 27 JAN 92 22:17:36 GMT
I've had problems with the mail so I'm repeating this. fink at acf5.nyu.edu (Howard Fink) writes: > Switch[expr,form1,val1,form2,val2,...] evaluates expr, matches it to form i, > and returns val i, if there's a match, Null otherwise. When I put in a list > {form1,val1,form2,val2,...} Mathematica treats it as a single object, and > for the life of me I can't strip off the curly brackets. The closest I came > was with Sequence, which replaced the curly brackets with parentheses. The following extended version of Switch deals with this problem and retains the HoldAll attribute of Switch; so does not force evaluation of all arguments. It may therefore be quicker for some applications. SetAttributes[SwitchList, HoldAll] SwitchList[expr_, {x___}] := Switch[expr,x] SwitchList[expr_,x___] := Switch[expr,x] If you don't mind altering Switch itself then you can extend it by Unprotect[Switch] Switch[expr_, {x___}] := Switch[expr,x] Protect[Switch] From Allan Hayes Department of Mathematics The University Leicester LE1 7RH U.K. hay at leicester.ac.uk