Huckleberry Febbo
Huckleberry Febbo

Reputation: 313

using quoted expressions and arrays in julia

I am having some trouble dealing with the differences between an array indexed at a spot and a the item at that spot in a quoted expression can be seen with this simple example:

julia> A=[:(2+3),:(4),:(9-8)];
julia> t=A[1];
julia> eval(quote 
       @show isequal($A[1],$t)
       @show $A[1]
       @show $t
       end)
isequal((Any[:(2 + 3),4,:(9 - 8)])[1],2 + 3) = false
(Any[:(2 + 3),4,:(9 - 8)])[1] = :(2 + 3)
2 + 3 = 5
5

I need to programatically access the indecies of A so I cannot simply use t for my application. So, something like this won't work:

julia> A=[:(2+3),:(4),:(9-8)];

julia> eval(quote
       for i in 1:2
       @show $(A[i])
       end
       end)
ERROR: UndefVarError: i not defined

But, for my application to work which is detailed here I need something (perhaps a temporary variable, which I tried unsuccessfully..) to equal t. Also, unfortunately I cannot just use eval. Thanks very much for any help.

Upvotes: 2

Views: 107

Answers (1)

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

You need to interpolate all of $(A[1]), instead of as now apply [1] on $A

eval(quote
          @show isequal($(A[1]),$t)
          @show $(A[1])
          @show $t
      end)
isequal(2 + 3, 2 + 3) = true
2 + 3 = 5
2 + 3 = 5
5

Upvotes: 2

Related Questions