Thanks, but its not quite what I'm looking for. I want to be able to edit 
the Expr tree and then evaluate different expressions using variables 
defined in the local scope,not the global scope (e.g. for genetic 
programming, where random changes to an expression are repeatedly evaluated 
to find the best one). Using anonymous functions could work but modifying 
the .code property of an anonymous function looks much more complex than 
modifying the Expr types.

Anyway thanks for your answer, maybe your suggestion is the only possible 
way to achieve this!

Mike 

On Thursday, December 11, 2014 6:56:15 PM UTC+13, Jameson wrote:
>
> eval, by design, doesn't work that way. there are just too many better 
> alternatives. typically, an anonymous function / lambda is the best and 
> most direct replacement:
>
> let x=2
>   println(x)            # Line 1
>   exp = () -> x+1
>   println(exp())    # Line 2
> end
>
>
> On Wed Dec 10 2014 at 10:43:00 PM Michael Mayo <mm...@waikato.ac.nz 
> <javascript:>> wrote:
>
>> Hi folks,
>>
>> I have the following code fragment:
>>
>> x=1
>> let x=2
>>   println(x)            # Line 1
>>   exp=:(x+1)
>>   println(eval(exp))    # Line 2
>> end
>>
>> It contains two variables both named x, one inside the scope defined by 
>> let, and one at global scope.
>>
>> If I run this code the output is:
>> 2
>> 2
>>
>> This indicates that (i) that line 1 is using the local version of x, and 
>> (ii) that line 2 is using the global version of x.
>>
>> If I remove this global x I now get an error because eval() is looking 
>> for the global x which no longer exists:
>>
>> let x=2
>>   println(x)            # Line 1
>>   exp=:(x+1)
>>   println(eval(exp))    # Line 2
>> end
>>
>> 2
>>
>> ERROR: x not defined
>>
>>
>> My question: when evaluating an expression using eval() such as line 2, 
>> how can I force Julia to use the local (not global) version of x and thus 
>> avoid this error?
>>
>>
>> Thanks
>>
>> Mike
>>
>

Reply via email to