Re: the simplest way?
- To: mathgroup at smc.vnet.net
- Subject: [mg16445] Re: the simplest way?
- From: Harald Giese <giese at dkrz.de>
- Date: Sat, 13 Mar 1999 02:21:51 -0500
- Organization: Institut fuer Meereskunde, Universitaet Hamburg
- References: <7c58om$7lr@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Antoine Zahnd wrote:
> ...
> 1) For example, how to write a function that will go thru the elements of a
> list of
> integers, and will return False as soon as an odd number is found, without
> using
> Length?
>
> With Length it is easy, but not so clean ...
> f[l_]:= Block[{k},For[k=1,k<=Length[l],k++,If[OddQ[l[[k]]],Return[True]]];
> False];
This works twice as fast:
f[l_] := If[Scan[If[OddQ[#],Return[True]]&,l],True,False,False]
If the likelyhood of odd numbers in your list is low, then
f[l_] := Apply[Or,OddQ[l]]
will work much faster.
Regards,
Harald