MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: List to infix

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113059] Re: List to infix
  • From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
  • Date: Tue, 12 Oct 2010 04:27:39 -0400 (EDT)

Hi,

what about making a string which can then be used with ToExpression?

MyEvaluate[l_List] := 
  ToExpression@StringJoin@Riffle[ToString /@ l, "~"];

MyEvaluate[{1, Plus, 3, Times, 5, Plus, 5}]


Cheers
Patrick


On Mon, 2010-10-11 at 05:16 -0400, Hugo Touchette wrote:
> Hi,
> 
> A problem about infix notation. In simple terms, I want to transform
> the list
> 
> {1, Plus, 3, Times, 5, Plus, 5}
> 
> into the infix form
> 
> 1~Plus~3~Times~5~Plus~5
> 
> to get a numerical result (here 25). Is there an easy way to do this?
> The lists I'm working with are actually much longer, so I want
> something efficient.
> 
> I've tried using Infix[mylist,"~"], but the ~ there is a string, not
> the infix operator. In the end, I simply wrote
> 
> ListOperation2[l_] := Module[{res, i},
>   res = l[[1]];
>   Do[
>    res = res~l[[i]]~l[[i + 1]]
>    , {i, 2, Length[l], 2}];
>   res
>   ]
> 
> This works, but I thought there might be a more efficient way to do
> things.
> 
> Thanks in advance for any help.
> 
> oguh
> 



  • Prev by Date: Re: Slow image import
  • Next by Date: Re: local variables - Module, For loop
  • Previous by thread: Re: List to infix
  • Next by thread: changing variables in place