Sophie

Sophie

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

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</title>
<link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/>
</head>
<body>
<table class="buttons">
  <tr>
    <td><div><a href="_local_spvariables_spin_spa_spfunction.html">next</a> | <a href="_using_spfunctions_spwith_spoptional_spinputs.html">previous</a> | <a href="_local_spvariables_spin_spa_spfunction.html">forward</a> | <a href="_using_spfunctions_spwith_spoptional_spinputs.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.html" title="">making functions</a></div>
<hr/>
<div><h1>making functions</h1>
<div>The operator <a href="_-_gt.html" title="make a function">-></a> is used to make new functions.  On its left we provide the names of the parameters to the function, and to the right we provide the body of the function, an expression involving those parameters whose value is to be computed when the function is applied.  Let's illustrate this by makint a function for squaring numbers and calling it <tt>sq</tt>.<table class="examples"><tr><td><pre>i1 : sq = i -> i^2

o1 = sq

o1 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i2 : sq 10

o2 = 100</pre>
</td></tr>
<tr><td><pre>i3 : sq(5+5)

o3 = 100</pre>
</td></tr>
</table>
When the function is evaluated, the argument is evaluated and assigned temporarily as the value of the parameter <tt>i</tt>.  In the example above, <tt>i</tt> was assigned the value <tt>10</tt>, and then the body of the function was evaluated, yielding <tt>100</tt>.<p/>
Here is how we make a function with more than one argument.<table class="examples"><tr><td><pre>i4 : tm = (i,j) -> i*j

o4 = tm

o4 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i5 : tm(5,7)

o5 = 35</pre>
</td></tr>
</table>
Functions can be used without assigning them to variables.<table class="examples"><tr><td><pre>i6 : (i -> i^2) 7

o6 = 49</pre>
</td></tr>
</table>
Another way to make new functions is to compose two old ones with the operator <a href="__at_at.html" title="a binary operator">@@</a>.<table class="examples"><tr><td><pre>i7 : sincos = sin @@ cos

o7 = sincos

o7 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i8 : sincos 2.2

o8 = -.555114915759425

o8 : RR (of precision 53)</pre>
</td></tr>
<tr><td><pre>i9 : sin(cos(2.2))

o9 = -.555114915759425

o9 : RR (of precision 53)</pre>
</td></tr>
</table>
Code that implements composition of functions is easy to write, because functions can create new functions and return them.  We illustrate this by writing a function called <tt>compose</tt> that will compose two functions, just as the operator <a href="__at_at.html" title="a binary operator">@@</a> did above.<table class="examples"><tr><td><pre>i10 : compose = (f,g) -> x -> f(g(x))

o10 = compose

o10 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i11 : sincos = compose(sin,cos)
--warning: function sincos redefined

o11 = sincos

o11 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i12 : cossin = compose(cos,sin)

o12 = cossin

o12 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i13 : sincos 2.2

o13 = -.555114915759425

o13 : RR (of precision 53)</pre>
</td></tr>
<tr><td><pre>i14 : cossin 2.2

o14 = .690586688560911

o14 : RR (of precision 53)</pre>
</td></tr>
</table>
We created two composite functions in the example above to illustrate an important point.  The parameters <tt>f</tt> and <tt>g</tt> acquire values when <tt>sincos</tt> is created, and they acquire different values when <tt>cossin</tt> is created.  These two sets of values do not interfere with each other, and the memory they occupy will be retained as long as they are needed.  Indeed, the body of both functions is <tt>x -> f(g(x))</tt>, and the only difference between them is the values assigned to the parameters <tt>f</tt> and <tt>g</tt>.<p/>
The class of all functions is <a href="___Function.html" title="the class of all functions">Function</a>.</div>
</div>
</body>
</html>