Message Boards Message Boards

0
|
3786 Views
|
4 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Very simple questions about adding multiple arguments...

Posted 10 years ago

In order to create a function that would add any number of arguments, I tried

f[_]=Plus
f[1,2,3]

and the output was f[1,2,3] instead of 6. I thought this had worked for me before... what should I do instead?

POSTED BY: Kacey Price
4 Replies
Posted 10 years ago

That's exactly what I ended up trying. Before, I kept trying to use conditions that weren't necessary; thanks for your help.

POSTED BY: Kacey Price

try:

ClearAll[MyPlus]
MyPlus[x___Integer] := Plus[x]
MyPlus[1]
MyPlus[1, 2, 3]
MyPlus[]

The ClearAll is not necessary, I'm just being careful ;)

POSTED BY: Sander Huisman
Posted 10 years ago

How would I set conditions for it to be only for integers? I tried IntegerQ, but I'm unsure of the format. I tried using IntegerQ, but I'm unsure how to format it. Thank you for answering my beginner's question. I appreciate it.

POSTED BY: Kacey Price
Posted 10 years ago

In[1]:= f = Plus

Out[1]= Plus

In[2]:= f[1, 2, 3]

Out[2]= 6

POSTED BY: Peter Fleck
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract