MathGroup Archive 2002

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

Search the Archive

Re: Problem using a loop to generate data for Point function

On Mon, 22 Jul 2002, Michael Carnright wrote:

> For[i = 1, i < 10, i++, Show[Graphics[Point[{i, i}]]]]
> This genereates nine graphics with one point each.  I was trying to
> generate one graphic with nine points.  I see my problem as calling
> the Point function nine times instead of only once.  I wish to use the
> data generated by the loop on a single graphic.

It looks as if you are coming from an imperative programming background.
Is the "for loop" a must?

Here is a suggestion on a functional solution:

Show[Graphics[Point[{#1, #1}] & /@ Range[1, 9]]]

i assume you are not familiar with some of the expressions above. 
Range[1, 9]

this returns a list with numbers 1 to 9.

now Point[{#1, #1}] & is a pure function. this means a function without a
name. usually you would write something like 
a = 1 + i

with a pure function you write (1 + #1)& here #1 takes the job of i and
the & is to denote that we are dealing with a pure function. 

to put some values into #1 you could map them. this is what the /@ does.

so we map the result from Range[1,9] onto the pure function 
Point[{#1,#1}]. to see that evaluate:

Point[{#1, #1}] & /@ Range[1, 9]

does this help ?

Oliver Ruebenkoenig, <ruebenko at>
   Phone: ++49 +761 203 7293

  • Prev by Date: Re: elementwise vector scalar subtraction
  • Next by Date: RE: Re: Q: Simplify with "much less" assumptions
  • Previous by thread: Re: Problem using a loop to generate data for Point function
  • Next by thread: Memory leak? Self-created non-executable Help notebook?