MathGroup Archive 2008

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

Search the Archive

Re: how to test where a list contains constant(s) or not

  • To: mathgroup at smc.vnet.net
  • Subject: [mg91984] Re: how to test where a list contains constant(s) or not
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Tue, 16 Sep 2008 19:22:20 -0400 (EDT)
  • Organization: University of Bergen
  • References: <gal3iu$e0a$1@smc.vnet.net>

Aya wrote:
> case1: { a, b, c, Pi }   gives true because of Pi
> case2: { a, b, c, 0.0001} gives true because of 0.0001
> case3: { a, b, c,  2 + Pi I } gives ture becase of 2 + Pi I
> case4: { a, b, c} gives false
> 
> is this function right ?
> 

Most of what you've written is completely redundant.

> ComplexQ[z_] := NumericQ[ z ] || ( NumericQ[ z ] && SameQ[ Head[ z ],
> Complex] )

This ComplexQ function is completely equivalent to NumericQ.  p || (p && 
q) is just p, right?

> IsConstantsIn[ lstList_ ] :=
> 		Module[ { intLength },
> 			intLength = Length@Select[ lstList, ComplexQ[ # ]& ];

One could just use ComplexQ instead of ComplexQ[#]&.

> 			If[ intLength > 0, Return[ True ], Return[ False ] ];
> 			Return[ False ];
> 			]
> 

You almost never need to use Return in Mathematica.  The above two lines 
are equivalent to the simple expression intLength > 0 in this context.

With all these simplifications your function becomes

f[lst_List] := Length@Select[lst, NumericQ] > 0

But the following approach is simpler and more efficient:

g[lst_List] := MemberQ[lst, _?NumericQ]


  • Prev by Date: Re: eliminate values while caculating Mean[data]
  • Next by Date: Re: Functional programming?
  • Previous by thread: Re: how to test where a list contains constant(s) or not
  • Next by thread: Re: how to test where a list contains constant(s) or not