Sophie

Sophie

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

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>mapping over lists -- apply a function to each element of a list</title>
<link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/>
</head>
<body>
<table class="buttons">
  <tr>
    <td><div><a href="_mapping_spover_sphash_sptables.html">next</a> | <a href="_for.html">previous</a> | <a href="_mapping_spover_sphash_sptables.html">forward</a> | <a href="_for.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="_mapping_spover_splists.html" title="apply a function to each element of a list">mapping over lists</a></div>
<hr/>
<div><h1>mapping over lists -- apply a function to each element of a list</h1>
<div>In programming, loops that operate on consecutive elements of a list are common, so we offer various ways to apply functions to each of the elements of a list, along with various ways to treat the returned values.<p/>
The most basic operation is provided by <a href="_scan.html" title="apply a function to each element">scan</a>, which applies a function consecutively to each element of a list, discarding the values returned.<table class="examples"><tr><td><pre>i1 : scan({a,b,c}, print)
a
b
c</pre>
</td></tr>
</table>
The keyword <a href="_break.html" title="break from a loop">break</a> can be used to terminate the scan prematurely, and optionally to specify a return value for the expression.  Here we use it to locate the first even number in a list.<table class="examples"><tr><td><pre>i2 : scan({3,5,7,11,44,55,77}, i -> if even i then break i)

o2 = 44</pre>
</td></tr>
</table>
The function <a href="_apply.html" title="apply a function to each element">apply</a> is similar to <a href="_scan.html" title="apply a function to each element">scan</a> but will produce a list containing the values returned.<table class="examples"><tr><td><pre>i3 : apply({1,2,3,4}, i -> i^2)

o3 = {1, 4, 9, 16}

o3 : List</pre>
</td></tr>
</table>
This operation is so common that we offer two shorthand notations for it, one with the function on the right and one with the function on the left.<table class="examples"><tr><td><pre>i4 : {1,2,3,4} / (i -> i^2)

o4 = {1, 4, 9, 16}

o4 : List</pre>
</td></tr>
<tr><td><pre>i5 : (i -> i^2) \ {1,2,3,4}

o5 = {1, 4, 9, 16}

o5 : List</pre>
</td></tr>
</table>
The associativity of these operators during parsing is set up so the following code works as one would wish.<table class="examples"><tr><td><pre>i6 : {1,2,3,4} / (i -> i^2) / (j -> 1000*j)

o6 = {1000, 4000, 9000, 16000}

o6 : List</pre>
</td></tr>
<tr><td><pre>i7 : (j -> 1000*j) \ (i -> i^2) \ {1,2,3,4}

o7 = {1000, 4000, 9000, 16000}

o7 : List</pre>
</td></tr>
<tr><td><pre>i8 : (j -> 1000*j) @@ (i -> i^2) \ {1,2,3,4}

o8 = {1000, 4000, 9000, 16000}

o8 : List</pre>
</td></tr>
</table>
The function <a href="_apply.html" title="apply a function to each element">apply</a> can also be used with two lists of the same length, in which case it will apply the function consecutively to corresponding elements of the two lists.<table class="examples"><tr><td><pre>i9 : apply({1,2,3}, {7,8,9}, (i,j) -> 1000*i+j)

o9 = {1007, 2008, 3009}

o9 : List</pre>
</td></tr>
</table>
The function <a href="_table.html" title="make a table (nested list)">table</a> can be used to create a table (doubly nested list) from two lists and a function of two arguments.  It applies the function consecutively to each element from the first list paired with each element from the second list, so the total number of evaluations of the function is the product of the lengths of the two lists.<table class="examples"><tr><td><pre>i10 : table({1,2,3},{7,8},(i,j) -> 1000*i+j)

o10 = {{1007, 1008}, {2007, 2008}, {3007, 3008}}

o10 : List</pre>
</td></tr>
</table>
The function <a href="_apply__Table.html" title="apply a function to elements of a table">applyTable</a> can be used to apply a function to each element of table.<table class="examples"><tr><td><pre>i11 : applyTable( {{1,2,3},{4,5}}, i -> i^2)

o11 = {{1, 4, 9}, {16, 25}}

o11 : List</pre>
</td></tr>
</table>
We may use <a href="_select.html" title="select from a list, hash table, or string">select</a> to select those elements from a list that satisfy some condition.  In the next example, we use the function <a href="_even.html" title="tell whether an integer is even">even</a> to select the even numbers from a list.<table class="examples"><tr><td><pre>i12 : select({1,2,3,4,5,6,7,8,9,10}, even)

o12 = {2, 4, 6, 8, 10}

o12 : List</pre>
</td></tr>
</table>
An optional first argument to <a href="_select.html" title="select from a list, hash table, or string">select</a> allows us to specify the maximum number of elements selected.<table class="examples"><tr><td><pre>i13 : select(2,{1,2,3,4,5,6,7,8,9,10}, even)

o13 = {2, 4}

o13 : List</pre>
</td></tr>
</table>
We may use <a href="_any.html" title="whether any elements satisfy a specified condition">any</a> to tell whether there is at least one element of a list satisfying a condition, and <a href="_all.html" title="whether all elements satisfy a specified condition">all</a> to tell whether all elements satisfy it.<table class="examples"><tr><td><pre>i14 : any({1,2,3,4,5,6,7,8,9,10}, even)

o14 = true</pre>
</td></tr>
<tr><td><pre>i15 : all({1,2,3,4,5,6,7,8,9,10}, even)

o15 = false</pre>
</td></tr>
</table>
We can use <a href="_position.html" title="find first element of a list satisfying a condition">position</a> to tell us the position of the first element in a list satisfying a condition.<table class="examples"><tr><td><pre>i16 : position({1,3,5,7,8,9,11,13,15,16},even)

o16 = 4</pre>
</td></tr>
</table>
The functions <a href="_fold.html" title="apply binary operator repeatedly">fold</a> and <a href="_accumulate.html" title="apply binary operator repeatedly">accumulate</a> provide various ways to apply a function of two arguments to the elements of a list.  One of the arguments is the next element from the list, and the other argument is the value returned by the previous application of the function.  As an example, suppose we want to convert the list <tt>{7,3,5,4,2}</tt> of digits into the corresponding number <tt>73542</tt>.  The formula <tt>(((7*10+3)*10+5)*10+4)+2</tt> is a fast way to do it that doesn't involve computing high powers of 10 separately.  We can do this with <a href="_fold.html" title="apply binary operator repeatedly">fold</a> and the following code.<table class="examples"><tr><td><pre>i17 : fold((i,j) -> i*10+j, {7,3,5,4,2})

o17 = 73542</pre>
</td></tr>
</table>
It is possible to give an additional argument to <a href="_fold.html" title="apply binary operator repeatedly">fold</a> so that lists of length 0 can be handled correctly.</div>
</div>
</body>
</html>