MathGroup Archive 1999

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

Search the Archive

Re: the simplest way ...?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg16490] Re: [mg16413] the simplest way ...?
  • From: David Withoff <withoff>
  • Date: Tue, 16 Mar 1999 03:59:44 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

> Hello,
> 
> I have two (basic) questions with lists, each time to avoid the Length
> function.
> 
> 1) For example, how to write a function that will go thru the elements of
> integers list,
> and will return False as soon as an odd number is found, without the use of
> the Length function?
> 
> 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];

f[l_] := Catch[Scan[If[OddQ[#], Throw[True]] &, l]; False]

or

f[l_] := (Scan[If[OddQ[#], Return[Return[True]]] &, l]; False)

> 2) Is it possible to write the following function without Length?
> 
> l={1,2,3};
> Table[ReplacePart[l,l[[i]]-1,i],{i,1,Length[l]}]
> {{0,2,3},{1,1,3},{1,2,2}}

MapIndexed[ReplacePart[l, l[[#2]][[1]] - 1, #2] &, l]

or

Map[l &, l] - DiagonalMatrix[Map[1 &, l]]

> More generally, is there a way to write something like:
> (for x in l ... collect (f x) ) , and the collection of the (f x) are
> returned in a list?

The Map function does this.

> I don't see how to write a Lisp style dolist function in Mathematica
> without the Length!
>
> Hope that the questions make sens ...
> 
> Thank you very much for your help.
> 
> antoine.zahnd at iprolink.ch

Dave Withoff
Wolfram Research


  • Prev by Date: Re: Re: planar polygon plot
  • Next by Date: Re: StartingParameters
  • Previous by thread: the simplest way ...?
  • Next by thread: RE: planar polygon plot