Message Boards Message Boards

1
|
1851 Views
|
4 Replies
|
6 Total Likes
View groups...
Share
Share this post:

How to check arguments so only 1 argument is accepted with CheckArguments?

Posted 11 months ago

I have a function and I want to check that only 1 argument is accepted. Is this the right way to use CheckArguments?

AliquotSequence // ClearAll
AliquotSequence::usage = "AliquotSequence[num] returns the aliquot sequence of num."
AliquotSequence[num_?PositiveIntegerQ] := 
 Catenate[
  FindTransientRepeat[
   NestWhileList[DivisorSigma[1, #] - # &, num, 
     DivisorSigma[1, #] - # != # &, 1, 99] /. {list___, 0} -> {list}, 
   2]]
   AliquotSequence[args___] := Null /; CheckArguments[AliquotSequence[args], 1] 

I based this on a response from ChatGPT 4, so I thought it might be worth testing to see if it was right. I tested a function with a similar definition and it seemed to work as intended. I looked at CheckArguments documentation but it was kind of confusing because it used an option, which I don't need.

POSTED BY: Peter Burbery
4 Replies

I looked at CheckArguments documentation but it was kind of confusing because it used an option, which I don't need.

Many functions have options, most of the time you don't need them and that is why they are optional. Why is that confusing?

Your usage of CheckArguments seems correct, and if it works for you then why not use it? It is certainly more straightforward than the resource function you linked.

POSTED BY: Jason Biggs

I just meant it was extra information. I relent in the criticism.

POSTED BY: Peter Burbery
Posted 11 months ago

Some other ways to do this on MSE.

POSTED BY: Rohit Namjoshi

There's a helpful ResourceFunction for this at https://resources.wolframcloud.com/FunctionRepository/resources/DeclareArgumentCount/. I am considering adding this to a paclet.

POSTED BY: Peter Burbery
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