I want to turn a sum like this
sum =a-b+c+d
Into a List like this:
sumToList[sum]={a,-b,c,d}
How can I achieve this?
I want to turn a sum like this
sum =a-b+c+d
Into a List like this:
sumToList[sum]={a,-b,c,d}
How can I achieve this?
List @@ sum
{a, -b, c, d}
From the docs on Apply (@@):
f@@expr replaces the head of expr by f.
So List@@sum replaces Head[sum] (that is, Plus) with List.
You can also get the same result by changing 0th Part of sum (which is its Head) to List:
sum[[0]] = List; sum
{a, -b, c, d}
List @@ 2*3 will result in {2,3}
– Alex DB
Feb 10 '17 at 17:10
List@@expr does change the head of expr, whatever it is, to List.
– kglr
Feb 10 '17 at 17:16
Try :
a - b + c + d /. Plus -> List
You can have a look at a - b + c + d //FullForm to see why this works.
Still another route:
Last[CoefficientArrays[#]] Variables[#] &[a - b + c + d]
{a, -b, c, d}