Hi,

you might try something like this:

function mapleaves(f,expr)
    if typeof(expr) == Expr
        expr.args[2:end]=map(e->mapleaves(f,e),expr.args[2:end])
        return expr
    else
        return :($f($expr))
    end
end

you can apply it like this:
eval(mapleaves(BigFloat,:(1+sin(1))))

I am by no means an expert in working on expressions, so feel free to post 
any corrections.

Paweł

W dniu piątek, 19 września 2014 16:39:43 UTC+2 użytkownik John Myles White 
napisał:
>
> Depth-first search and replace?
>
>  — John
>
> On Sep 19, 2014, at 7:32 AM, David P. Sanders <[email protected] 
> <javascript:>> wrote:
>
>
>
> El viernes, 19 de septiembre de 2014 09:26:09 UTC-5, David P. Sanders 
> escribió:
>>
>>
>>
>> El viernes, 19 de septiembre de 2014 08:58:56 UTC-5, Isaiah escribió:
>>>
>>> To do what you want, very briefly:
>>>
>>> > x.args[3] == :pi && x.args[3] = Expr(:call, :BigFloat, :pi)
>>>
>>> This will make more sense if you look at `xdump` output to see how Exprs 
>>> are structured
>>> (and `xdump` is quite handy to know about in general). Try:
>>>
>>>
>>> > xdump(:(big(3) + big(pi)))
>>> ...
>>>
>>>
>> Yes, that's what I need, thanks. Now I need to iterate over the (in 
>> principle arbitrarily complex, i.e. nested) syntax tree and do this 
>> everywhere. 
>> Is there a standard method for this kind of iteration?
>>
>
> I guess some kind of recursion. I'll give it a go...
>
>  
>
>>  
>>
>>>
>>> On Fri, Sep 19, 2014 at 9:42 AM, David P. Sanders <[email protected]> 
>>> wrote:
>>>
>>>>
>>>>
>>>> El viernes, 19 de septiembre de 2014 08:34:05 UTC-5, David P. Sanders 
>>>> escribió:
>>>>>
>>>>> Hi, for a package I'm writing (or, more precisely, trying to write), I 
>>>>> need to do the following:
>>>>>
>>>>> Change `:(3 + pi)` 
>>>>> into `:(3 + big(pi))`  
>>>>> or `:(BigFloat(3) + BigFloat(pi))`
>>>>>
>>>>
>>>> I forgot to say that I need to do this so that I can perform the 
>>>> calculations with different rounding modes.
>>>>  
>>>>
>>>>>
>>>>> Basically I need to apply `BigFloat` to every symbol in the 
>>>>> expression. (I believe that it would be sufficient to wrap `MathConst`s 
>>>>> in 
>>>>> `big`, but converting everything to BigFloat is probably a good idea.)
>>>>>
>>>>> This is my first foray into parsing etc. I'm guessing there's a 
>>>>> package that allows me to do this easily -- could someone please point it 
>>>>> out?
>>>>> Or how should I go about this?
>>>>>
>>>>> In principle the expressions could be more complicated, e.g.  `(3 + 
>>>>> pi) * (2.5 - e)`
>>>>>
>>>>> Thanks,
>>>>> David.
>>>>>
>>>>
>>>
>

Reply via email to