Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 7ebd25ac536d248d499a3ce2acda963a > files > 4691

Macaulay2-1.3.1-8.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8" ?>  <!-- for emacs: -*- coding: utf-8 -*- -->
<!-- Apache may like this line in the file .htaccess: AddCharset utf-8 .html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"	 "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>making functions with a variable number of arguments</title>
<link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/>
</head>
<body>
<table class="buttons">
  <tr>
    <td><div><a href="_making_spfunctions_spwith_spmultiple_spreturn_spvalues.html">next</a> | <a href="_local_spvariables_spin_spa_spfunction.html">previous</a> | <a href="_making_spfunctions_spwith_spmultiple_spreturn_spvalues.html">forward</a> | <a href="_local_spvariables_spin_spa_spfunction.html">backward</a> | <a href="___The_sp__Macaulay2_splanguage.html">up</a> | <a href="index.html">top</a> | <a href="master.html">index</a> | <a href="toc.html">toc</a> | <a href="http://www.math.uiuc.edu/Macaulay2/">Macaulay2 web site</a></div>

    </td>
  </tr>
</table>
<div><a href="index.html" title="">Macaulay2Doc</a> > <a href="___The_sp__Macaulay2_splanguage.html" title="">The Macaulay2 language</a> > <a href="_making_spfunctions_spwith_spa_spvariable_spnumber_spof_sparguments.html" title="">making functions with a variable number of arguments</a></div>
<hr/>
<div><h1>making functions with a variable number of arguments</h1>
<div>It is easy to write a function with a variable number of arguments.  Define the function with just one parameter, with no parentheses around it.  If the function is called with several arguments, the value of the single parameter will be a sequence containing the several arguments; if the function is called with one argument, the value of the parameter will be that single argument.<table class="examples"><tr><td><pre>i1 : f = x -> {class x, if class x === Sequence then #x};</pre>
</td></tr>
<tr><td><pre>i2 : f()

o2 = {Sequence, 0}

o2 : List</pre>
</td></tr>
<tr><td><pre>i3 : f(3)

o3 = {ZZ, }

o3 : List</pre>
</td></tr>
<tr><td><pre>i4 : f(3,4)

o4 = {Sequence, 2}

o4 : List</pre>
</td></tr>
<tr><td><pre>i5 : f(3,4,5)

o5 = {Sequence, 3}

o5 : List</pre>
</td></tr>
</table>
We could use the function <a href="_sequence.html" title="make a sequence">sequence</a> to bring the case where there is just one argument into line with the others.  It will enclose anything that is not a sequence in a sequence of length one.<table class="examples"><tr><td><pre>i6 : f = x -> (
          x = sequence x;
          {class x, #x});
--warning: function f redefined</pre>
</td></tr>
<tr><td><pre>i7 : f()

o7 = {Sequence, 0}

o7 : List</pre>
</td></tr>
<tr><td><pre>i8 : f(3)

o8 = {Sequence, 1}

o8 : List</pre>
</td></tr>
<tr><td><pre>i9 : f(3,4)

o9 = {Sequence, 2}

o9 : List</pre>
</td></tr>
<tr><td><pre>i10 : f(3,4,5)

o10 = {Sequence, 3}

o10 : List</pre>
</td></tr>
</table>
As an aside, we reveal that there is a way to define a function of one argument that will signal an error if it's given more than one argument: put parentheses around the single parameter in the definition of the function.  As a side effect it can be used to extract the single element from a singleton sequence.<table class="examples"><tr><td><pre>i11 : ((x) -> x) 3

o11 = 3</pre>
</td></tr>
<tr><td><pre>i12 : 1 : 3

o12 = 1 : (3)

o12 : Sequence</pre>
</td></tr>
<tr><td><pre>i13 : ((x) -> x) oo

o13 = 3</pre>
</td></tr>
</table>
</div>
</div>
</body>
</html>