Skip to main content
Post Closed as "exact duplicate" by rm -rf
Tweeted twitter.com/#!/StackMma/status/320127498149060608
added picture
Source Link
cormullion
  • 24.3k
  • 4
  • 64
  • 136

why Why the coloring in blockBlock construct?

I want a function that works like in the following example: it gets an expression which contains a variable var and evaluate it setting setting var = value. 

I used BlockBlock to avoid expr being evaluated if variable var holds a value in the notebook. It seems that my function works correctly - I made aloalso a trace - but I get var inside block coloured red and I think it's a warning from MathematicaMathematica. Can you explain to me why I obtain it? Is my code wrong? Is there a better way to obtain the desired behavior?

SetAttributes[myFunc, HoldFirst];

myFunc[{expr_, var_}, value_] :=
  Block[{var}, expr /. var :> value]

Thanks to all, Andreapicture

why the coloring in block construct

I want a function that works like in the following example: it gets an expression which contains a variable var and evaluate it setting setting var = value. I used Block to avoid expr being evaluated if variable var holds a value in the notebook. It seems that my function works correctly - I made alo a trace - but I get var inside block coloured red and I think it's a warning from Mathematica. Can you explain to me why I obtain it? Is my code wrong? Is there a better way to obtain the desired behavior?

SetAttributes[myFunc, HoldFirst];

myFunc[{expr_, var_}, value_] :=
  Block[{var}, expr /. var :> value]

Thanks to all, Andrea

Why the coloring in Block construct?

I want a function that works like in the following example: it gets an expression which contains a variable var and evaluate it setting setting var = value. 

I used Block to avoid expr being evaluated if variable var holds a value in the notebook. It seems that my function works correctly - I made also a trace - but I get var inside block coloured red and I think it's a warning from Mathematica. Can you explain to me why I obtain it? Is my code wrong? Is there a better way to obtain the desired behavior?

SetAttributes[myFunc, HoldFirst];

myFunc[{expr_, var_}, value_] :=
  Block[{var}, expr /. var :> value]

picture

added 2 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

I want a function that works like in the following example: it gets an expression which contains a variable var and evaluate it setting setting var = value. I used Block to avoid expr being evaluated if variable var holds a value in the notebook. It seems that my function works correctly - I made alo a trace - but I get var inside block coloured red and I think it's a warning from Mathematica. Can you explain to me why I obtain it? Is my code wrong? Is there a better way to obtain the desired behavior?

SetAttributes[myFunc, HoldFirst;HoldFirst];
  
myFunc[{expr_, var_}, value_] :=
  Block[{var}, expr /. var :> value]

Thanks to all, Andrea

I want a function that works like in the following example: it gets an expression which contains a variable var and evaluate it setting setting var = value. I used Block to avoid expr being evaluated if variable var holds a value in the notebook. It seems that my function works correctly - I made alo a trace - but I get var inside block coloured red and I think it's a warning from Mathematica. Can you explain to me why I obtain it? Is my code wrong? Is there a better way to obtain the desired behavior?

SetAttributes[myFunc, HoldFirst;
 myFunc[{expr_, var_}, value_] :=
  Block[{var}, expr /. var :> value]

Thanks to all, Andrea

I want a function that works like in the following example: it gets an expression which contains a variable var and evaluate it setting setting var = value. I used Block to avoid expr being evaluated if variable var holds a value in the notebook. It seems that my function works correctly - I made alo a trace - but I get var inside block coloured red and I think it's a warning from Mathematica. Can you explain to me why I obtain it? Is my code wrong? Is there a better way to obtain the desired behavior?

SetAttributes[myFunc, HoldFirst];
 
myFunc[{expr_, var_}, value_] :=
  Block[{var}, expr /. var :> value]

Thanks to all, Andrea

Source Link

why the coloring in block construct

I want a function that works like in the following example: it gets an expression which contains a variable var and evaluate it setting setting var = value. I used Block to avoid expr being evaluated if variable var holds a value in the notebook. It seems that my function works correctly - I made alo a trace - but I get var inside block coloured red and I think it's a warning from Mathematica. Can you explain to me why I obtain it? Is my code wrong? Is there a better way to obtain the desired behavior?

SetAttributes[myFunc, HoldFirst;
 myFunc[{expr_, var_}, value_] :=
  Block[{var}, expr /. var :> value]

Thanks to all, Andrea