Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's one way:</p> <pre><code>In[1]:= foo[args___] := Length[{args}] In[2]:= foo[1,2,3,4] Out[2]= 4 </code></pre> <p>When you define a function like this, the pattern <code>args___</code> (with 3 trailing underscores) will match a <a href="http://reference.wolfram.com/mathematica/ref/Sequence.html" rel="nofollow noreferrer"><code>Sequence</code></a> of 0 or more things. You can't use <code>Length</code> on a <code>Sequence</code> and have anything sensible happen, so you should wrap <code>args</code> in a <code>List</code> (the <code>{}</code>) first.</p> <p>However, belisarius is correct. For a lot of iterative operations, it will be easier and more efficient to use built-in higher-order functions like <code>Map</code> and <code>Fold</code>.</p> <p><strong>EDIT to add:</strong> Due to way that Mathematica expressions are built on top of bounds-checked arrays, <code>Length</code> is <em>O</em> (1) in time. This might lead you to believe that <code>foo</code> <strong>also</strong> has <em>O</em> (1) complexity, but you would be wrong. Due to the way pattern-matching works, all of the elements matched by <code>args</code> will be <strong>copied</strong> into the new <code>List</code> that you then pass to <code>Length</code>, making the complexity <em>O</em> (<em>N</em>). This isn't necessarily a huge problem, because using really huge argument lists with a function almost invariably means using <code>Apply</code>, which does an <em>O</em> (<em>N</em>) copy anyway, but it's something you should know.</p> <p><strong>EDIT again to add:</strong> There's another way to do this using <code>Length</code> directly on the expression being evaluated (like most of Mathematica's list-oriented functions, <code>Length</code> can be used on expressions with any head, not just lists). Nothing is copied because no sequences are matched and given new heads, and the function which is having its arguments counted need not have any special attributes like <code>HoldAll</code>. Nonetheless, it is a sleazy hack that exploits a quirk in the pattern-matching machinery by introducing side-effects where side-effects really don't belong, so I would use it with extreme caution, if at all:</p> <pre><code>Module[{n}, expr : foo[___] /; (n = Length[Unevaluated[expr]]; True) := n] </code></pre> <p>The variable <code>n</code> could be global, but <code>Module</code> will create (or at least do a good job faking) lexical closures, so you can at least keep your variables local.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload