Sophie

Sophie

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

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 new functions with optional arguments</title>
<link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/>
</head>
<body>
<table class="buttons">
  <tr>
    <td><div><a href="_what_spa_spclass_spis.html">next</a> | <a href="_making_spfunctions_spwith_spmultiple_spreturn_spvalues.html">previous</a> | <a href="_what_spa_spclass_spis.html">forward</a> | <a href="_making_spfunctions_spwith_spmultiple_spreturn_spvalues.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_spnew_spfunctions_spwith_spoptional_sparguments.html" title="">making new functions with optional arguments</a></div>
<hr/>
<div><h1>making new functions with optional arguments</h1>
<div>Let's consider an example where we wish to construct a linear function of <tt>x</tt> called <tt>f</tt>, with the slope and y-intercept of the graph being optional arguments of <tt>f</tt>.  We use the <a href="__gt_gt.html" title="a binary operator, uses include bit shifting, or attaching optional inputs to functions">>></a> operator to attach the default values to our function, coded in a special way.<table class="examples"><tr><td><pre>i1 : opts = {Slope => 1, Intercept => 1}

o1 = {Slope => 1, Intercept => 1}

o1 : List</pre>
</td></tr>
<tr><td><pre>i2 : f = opts >> o -> x -> x * o.Slope + o.Intercept

o2 = f

o2 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i3 : f 5

o3 = 6</pre>
</td></tr>
<tr><td><pre>i4 : f(5, Slope => 100)

o4 = 501</pre>
</td></tr>
<tr><td><pre>i5 : f(5, Slope => 100, Intercept => 1000)

o5 = 1500</pre>
</td></tr>
</table>
In the example the function body is the code <tt>x * opts.Slope + opts.Intercept</tt>.  When it is evaluated, a hash table is assigned to <tt>opts</tt>; its keys are the names of the optional arguments, and the values are the corresponding current values, obtained either from the default values specified in the definition of <tt>f</tt>, or from the options specified at the time <tt>f</tt> is called.<p/>
In the example above, the inner function has just one argument, <tt>x</tt>, but handling multiple arguments is just as easy.  Here is an example with two arguments.<table class="examples"><tr><td><pre>i6 : f = {a => 1000} >> o -> (x,y) -> x * o.a + y;
--warning: function f redefined</pre>
</td></tr>
<tr><td><pre>i7 : f(3,7)

o7 = 3007</pre>
</td></tr>
<tr><td><pre>i8 : f(5,11,a=>10^20)

o8 = 500000000000000000011</pre>
</td></tr>
</table>
</div>
</div>
</body>
</html>