Function - Revision historyhttps://wiki.haskell.org/index.php?title=Function&action=history
Revision history for this page on the wikienMediaWiki 1.19.14+dfsg-1Sun, 02 Aug 2015 18:39:48 GMTGtirloni: fixed dead linkhttps://wiki.haskell.org/index.php?title=Function&diff=42839&oldid=prev
https://wiki.haskell.org/index.php?title=Function&diff=42839&oldid=prev<p>fixed dead link</p>
<table class='diff diff-contentalign-left'>
<tr valign='top'>
<td colspan='1' style="background-color: white; color:black;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black;">Revision as of 17:28, 10 November 2011</td>
</tr></table>Thu, 10 Nov 2011 17:28:31 GMTGtirlonihttps://wiki.haskell.org/Talk:FunctionYmotongpoo at 15:03, 5 December 2009https://wiki.haskell.org/index.php?title=Function&diff=32158&oldid=prev
https://wiki.haskell.org/index.php?title=Function&diff=32158&oldid=prev<p></p>
<table class='diff diff-contentalign-left'>
<tr valign='top'>
<td colspan='1' style="background-color: white; color:black;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black;">Revision as of 15:03, 5 December 2009</td>
</tr></table>Sat, 05 Dec 2009 15:03:33 GMTYmotongpoohttps://wiki.haskell.org/Talk:FunctionBrettGiles: Fix linkhttps://wiki.haskell.org/index.php?title=Function&diff=7112&oldid=prev
https://wiki.haskell.org/index.php?title=Function&diff=7112&oldid=prev<p>Fix link</p>
<table class='diff diff-contentalign-left'>
<tr valign='top'>
<td colspan='1' style="background-color: white; color:black;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black;">Revision as of 00:16, 19 October 2006</td>
</tr></table>Thu, 19 Oct 2006 00:16:58 GMTBrettGileshttps://wiki.haskell.org/Talk:FunctionBrettGiles: HaWiki conversionhttps://wiki.haskell.org/index.php?title=Function&diff=6875&oldid=prev
https://wiki.haskell.org/index.php?title=Function&diff=6875&oldid=prev<p>HaWiki conversion</p>
<p><b>New page</b></p><div>[[Category:Glossary]] [[Category:Language]]<br />
Mathematically speaking, a [http://mathworld.wolfram.com/Function.html function] relates all values in a set &lt;math&gt;A&lt;/math&gt; to values in a set &lt;math&gt;B&lt;/math&gt;. The function &lt;math&gt;square\ x = x^2&lt;/math&gt;, given that &lt;math&gt;x&lt;/math&gt; is an integer, will map all elements of the set of integers into another set -- in this case the set of square integers. In Haskell functions can be specified as below in the examples, with an optional [[Type specification|type specification] that gives the compiler (and other programmers) a hint as to the use of the function. <br />
<br />
==Examples==<br />
&lt;haskell&gt;<br />
square :: Int -&gt; Int<br />
square x = x * x<br />
&lt;/haskell&gt;<br />
<br />
In other words, a function has ''input'' and ''output'', and it describes how to produce the output from its input. Functions can be ''applied'', which just means that you give an input value as argument to the function and can then expect to receive the corresponding output value.<br />
<br />
Haskell functions are ''first class'' entities, which means that they<br />
* can be given names<br />
* can be the value of some expression<br />
* can be members of a list<br />
* can be elements of a tuple<br />
* can be passed as parameters to a function<br />
* can be returned from a function as a result<br />
(quoted from Davie's ''Introduction to Functional Programming Systems using Haskell.'')<br />
===&lt;hask&gt;map&lt;/hask&gt; example===<br />
As an example of the power of first-class functions, consider the function ''map'': <br />
&lt;haskell&gt;<br />
map :: (a -&gt; b) -&gt; [a] -&gt; [b]<br />
map f xs = [f x | x &lt;- xs]<br />
&lt;/haskell&gt;<br />
<br />
(Note this is a [[Higher order function]].)<br />
This function takes two arguments: a function ''f'' which maps ''a''s to ''b''s, and a list ''xs'' of ''a''s. It returns a list of ''b''s which are the results of applying ''f'' to every member of ''xs''. So &lt;hask&gt;map square [1,1,2,3,5,8]&lt;/hask&gt; would yield the list &lt;hask&gt;[1,1,4,9,25,64]&lt;/hask&gt;. When you realize that the list of ''b''s that ''map'' returns can itself be a list of functions, things start to get interesting.<br />
<br />
Suppose you have some data structure (e.g. ''Set'') that has the function &lt;hask&gt;insert :: Int -&gt; Set -&gt; Set&lt;/hask&gt;, which takes an integer and a set, and returns the set created by inserting the given integer into the given set. And suppose you have ''mySet'' and ''myList,'' a set and a list of values to be added to the set, respectively. One could write a function to recurse over the list of integers, each time inserting a single member of ''myList,'' but with first-class functions this is not necessary. Look at the expression &lt;hask&gt;map insert myList&lt;/hask&gt; -- what is the type of the list which it produces? Since ''insert'' takes an ''Int'' and a ''Set'', but only ''Int''s were given, the resulting list will be of functions that take a set and return a set. Conceptually, the code &lt;hask&gt;map insert [1,2,3]&lt;/hask&gt; will return the list &lt;hask&gt;[(insert 1) (insert 2) (insert 3)]&lt;/hask&gt;. <br />
<br />
===Composition / folding example===<br />
Haskell supports a [[Function composition]] operator: <br />
&lt;haskell&gt;<br />
(.) :: (b -&gt; c) -&gt; (a -&gt;b) -&gt; (a-&gt;c)<br />
(f . g) x = f (g x) <br />
&lt;/haskell&gt;<br />
So, for example, &lt;hask&gt;((insert 1) . (insert 2) . (insert 3)) mySet&lt;/hask&gt; is the same as &lt;hask&gt;insert 1 (insert 2 (insert 3 mySet))&lt;/hask&gt;. We're almost there -- what we need now is a function that can automatically put the composition operator between every element of &lt;hask&gt;map insert myList&lt;/hask&gt;. Such code is included in Haskell, and it's known as ''folding.''<br />
<br />
Several variants of the ''fold'' function are defined, but the basic concept is the same: given a function and a list, &quot;collapse&quot; the list by applying the function &quot;between&quot; its elements. This is easiest to see with simple binary operators, but it is not limited to them. For example, &lt;hask&gt;foldr1 (+) [1,1,2,3,5]&lt;/hask&gt; eventually creates the expression &lt;hask&gt;1+1+2+3+5&lt;/hask&gt;, and thus returns 12. In the set example, &lt;hask&gt;foldr1 (.) (map insert myList)&lt;/hask&gt; gives us what we want, the successive insertion of every element of ''myList.'' What is the type of this expression? It is &lt;hask&gt;Set -&gt; Set&lt;/hask&gt;, meaning it will take a set and return a set -- in this case, the set it returns will have every element of ''myList'' inserted. To complete the example, <br />
&lt;haskell&gt;<br />
newSet = (foldr1 (.) (map insert myList)) mySet <br />
&lt;/haskell&gt;<br />
will define ''newSet'' as ''mySet'' with the elements of ''myList'' inserted. <br />
<br />
==See also==<br />
* The [http://haskell.cs.yale.edu/tutorial/functions.html Functions section] in the Gentle Introduction.</div>Tue, 10 Oct 2006 23:58:04 GMTBrettGileshttps://wiki.haskell.org/Talk:Function