You can do this just fine, but you have to be explicit about what variables
you want to pass in, e.g.

let x=2
  exp=:(x+1)
  eval(:(let x = $x; $exp; end))
end

If you want to call the expression with multiple inputs, wrap it in a
function:

let x=2
  exp=:(x+1)
  f = eval(:(x -> $exp))
  f(x)
end


On 11 December 2014 at 06:32, Jameson Nash <vtjn...@gmail.com> wrote:

> I'm not quite sure what a genetic program of that sort would look like. I
> would be interested to hear if you get something out of it.
>
> Another alternative is to use a module as the environment:
>
> module MyEnv
> end
> eval(MyEnv, :(code block))
>
> This is (roughly) how the REPL is implemented to work.
>
> On Thu Dec 11 2014 at 1:26:57 AM Michael Mayo <mm...@waikato.ac.nz> wrote:
>
>> 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>
>>> 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