1

Let's say I have a list of arguments, of any length.

How would I add them all together?

f = Evaluate[Plus @@ Array[Slot, 3]] &
f[1, 2, 3]

This would add together 3 numbers, but how would I add together any amount of numbers?

Zach
  • 11
  • 1

2 Answers2

5

See SlotSequence. I would use:

f = +## &

f[1, 2, 3]
6

The long form:

InputForm[f]
Plus[##1] &

Other forms would be:

Total @ {##} &

Tr @ {##} &

Plus @@ {##} &
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
3

I'm not sure I'm interpreting your question correctly, but the most obvious way is just

f = Plus;
f[1, 2, 3, 4, 5]

I assume that there is a deeper use case behind your question, so when you clarify what you like to achieve, maybe someone can give you better tips.

halirutan
  • 112,764
  • 7
  • 263
  • 474