wolfram mathematica - Adding the last two elements of a list -
in mathematica, cleanest way of taking list
{r1, r2, r3, ..., rn, a, b} and returning
{r1, r2, r3, ..., rn, + b} or more
{r1, r2, r3, ..., rn, f[a, b]} for function f?
lst = {a[1], a[2], a[3], a[4], a[5], a[6], a[7], a, b}; lst /. {a___, b_, c_} -> {a, f[b, c]} ==> {a[1], a[2], a[3], a[4], a[5], a[6], a[7], f[a, b]} or (ugly):
append[take[lst, {1, -3}], f @@ lst[[{-2, -1}]]]
Comments
Post a Comment