From: Yaroslav Bulatov on
First one works, but second one doesn't, why?
Block[{n = 2}, Plot[n x, {x, 1, 2}]]
Block[{n = 2}, #] &@Plot[n x, {x, 1, 2}]

From: James Stein on
Block[{n = 2}, # &@Plot[n x, {x, 1, 2}]]
Fixed that for you! (misplaced brackets)

On Fri, Jul 30, 2010 at 3:55 AM, Yaroslav Bulatov <yaroslavvb(a)gmail.com> wrote:
> First one works, but second one doesn't, why?
> Block[{n = 2}, Plot[n x, {x, 1, 2}]]
> Block[{n = 2}, #] &@Plot[n x, {x, 1, 2}]
>
>

From: Bill Rowe on
On 7/30/10 at 6:55 AM, yaroslavvb(a)gmail.com (Yaroslav Bulatov) wrote:

>First one works, but second one doesn't, why?
>Block[{n = 2}, Plot[n x, {x, 1, 2}]]
>Block[{n = 2}, #] &@Plot[n x, {x, 1, 2}]

In the first instance, Block makes n a local variable and
assigns a numeric value to it. This makes the argument to 2 x
which works fine.

In the second instance, the Plot command is evaluated first. In
this case, n has no assigned value so Plot cannot determine how
to plot the curve and plots nothing. The variable n does not get
its assigned value until after Plot has been executed.

You can get something like the second instance to work by
preventing Plot from being evaluated first as follows:

Block[{n = 2}, #] &@Unevaluated(a)Plot[n x, {x, 1, 2}]

But this seems like a rather convoluted way to do things.


From: Themis Matsoukas on
You can tell from the syntax coloring that the value of n is not passed into Plot. That's because the # is separated from & by]. These two variations work:

A = # &@Plot;
Block[{n = 2}, A[n x , {x, 1, 2}]]


> First one works, but second one doesn't, why?
> Block[{n = 2}, Plot[n x, {x, 1, 2}]]
> Block[{n = 2}, #] &@Plot[n x, {x, 1, 2}]
>

From: Murray Eisenberg on
I don't think the O.P. had in mind asking for a fix. I think he was, in
essence, trying to create a pure function with Block...

f = Block[{n = 2}, #]&

.... and then trying to evaluate that with argument Plot[n x, {x,1,2}].
He did this in one step, of course--the second of his two code lines.

The trouble, of course, is that the n in Block[{n = 2}, #]& is now
localized to that Block, whereas the n in Plot[n x, {x,1,2}] is not
localized in that Plot.

On 7/31/2010 2:39 AM, James Stein wrote:
> Block[{n = 2}, #&@Plot[n x, {x, 1, 2}]]
> Fixed that for you! (misplaced brackets)
>
> On Fri, Jul 30, 2010 at 3:55 AM, Yaroslav Bulatov<yaroslavvb(a)gmail.com> wrote:
>> First one works, but second one doesn't, why?
>> Block[{n = 2}, Plot[n x, {x, 1, 2}]]
>> Block[{n = 2}, #]&@Plot[n x, {x, 1, 2}]
>>
>>
>

--
Murray Eisenberg murray(a)math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305