MathGroup Archive 2005

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

Search the Archive

Re: To be or not to be...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg62092] Re: [mg62002] To be or not to be...
  • From: "Ingolf Dahl" <ingolf.dahl at telia.com>
  • Date: Fri, 11 Nov 2005 02:51:55 -0500 (EST)
  • Reply-to: <ingolf.dahl at telia.com>
  • Sender: owner-wri-mathgroup at wolfram.com

Thanks for all answers.
The behaviour of Sequence together with Equal is of course a feature and not
a bug. But it just baffled my naïve intuition, and that means that there was
something more to learn about Mathematica. Maybe someone else also might
find these examples illuminating (or useful in teaching), but they maybe are
too trivial for the real gurus.

The behaviour of Equal and Unequal when the number of arguments is different
from two can evidently be simulated in the following way, where we start
with the definitions for two arguments: 

myequal[x___] := And @@ Map[Apply[Equal, #] &, Subsets[{x}, {2}]];
myunequal[x___] := And @@ Map[Apply[Unequal, #] &, Subsets[{x}, {2}]];

With these definitions the behaviour for zero and one argument is logical.
If the number of arguments is different from two, Unequal is not equivalent
to (Not @ Equal), a design choice probably made to increase the flexibility
of the language.

I think one simple way to test if a variable x is set to Sequence[] is to
check if Length[{x}]==0. 

Here are some more features (You do not need to explain to me why they work
this way):

If[3 < 4, Sequence[], 57] evaluates to 57

but 

Set[g]; If[3 < 4, g, 57] evaluates to Sequence[]

Equal[Sequence[a, a, a], Sequence[a, a, a, a, a, a, a]] evaluates to True,
while

Unequal[Sequence[a, a, a], Sequence[a, a, a, a, a, a, a]] evaluates to
False.

Here is a case where the behaviour of Sequence is hidden. This could
constitute a real elephant trap:
Define the innocent function

f[x___] := If[x == 666, Print["The argument of F is Equal to 666"]]

Evaluate now f[] and f[666,666,666,666]!

Best regards

Ingolf Dahl
http://web.telia.com/~u31815170/Mathematica/ 

 

-----Original Message-----
From: albert [mailto:awnl at arcor.de] 
To: mathgroup at smc.vnet.net
Subject: [mg62092] [mg62049] Re: To be or not to be...

Ingolf Dahl wrote:

> 
> To Mathgroup,
> 
> Try the following commands in order:
> 
> b = Sequence[]
> 
> b == Sequence[]
> 
> b != Sequence[]
> 
> b === Sequence[]
> 
> b =!= Sequence[]
> 
> The four last lines all evaluate to "True" for me. Quite amazing!

Was that a question? The reason is that Equal[], SameQ[], UnsameQ[],
Unequal[] all return True if called with zero (or one) arguments, which is
in fact what you are doing, since Sequence[] does what it is meant to do,
no matter whether the head of the expression is List, Equal or UnsameQ. If
you want to test whether something is a Sequence[] or not you will need
another approach, a simple way is to use a head with Attribute
SequenceHold:

SetAttributes[ccc, SequenceHold]
ccc[b] =!= ccc[Sequence[]]
ccc[b] === ccc[Sequence[]]

which will give the results you probably expected...

albert




  • Prev by Date: Re: Re: Timing runs for the last part of my previous post
  • Next by Date: Re: Re: ((a&&b)||c)==((a||c)&&(b||c))
  • Previous by thread: Re: To be or not to be...
  • Next by thread: Write, Put,..