Function - Revision history
https://wiki.haskell.org/index.php?title=Function&action=history
Revision history for this page on the wikienMediaWiki 1.19.14+dfsg-1Sat, 23 Jul 2016 21:21:32 GMTGtirloni: fixed dead link
https://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 2009
https://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 link
https://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 conversion
https://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 <math>A</math> to values in a set <math>B</math>. The function <math>square\ x = x^2</math>, given that <math>x</math> 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 />
<haskell><br />
square :: Int -> Int<br />
square x = x * x<br />
</haskell><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 />
===<hask>map</hask> example===<br />
As an example of the power of first-class functions, consider the function ''map'': <br />
<haskell><br />
map :: (a -> b) -> [a] -> [b]<br />
map f xs = [f x | x <- xs]<br />
</haskell><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 <hask>map square [1,1,2,3,5,8]</hask> would yield the list <hask>[1,1,4,9,25,64]</hask>. 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 <hask>insert :: Int -> Set -> Set</hask>, 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 <hask>map insert myList</hask> -- 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 <hask>map insert [1,2,3]</hask> will return the list <hask>[(insert 1) (insert 2) (insert 3)]</hask>. <br />
<br />
===Composition / folding example===<br />
Haskell supports a [[Function composition]] operator: <br />
<haskell><br />
(.) :: (b -> c) -> (a ->b) -> (a->c)<br />
(f . g) x = f (g x) <br />
</haskell><br />
So, for example, <hask>((insert 1) . (insert 2) . (insert 3)) mySet</hask> is the same as <hask>insert 1 (insert 2 (insert 3 mySet))</hask>. We're almost there -- what we need now is a function that can automatically put the composition operator between every element of <hask>map insert myList</hask>. 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, "collapse" the list by applying the function "between" its elements. This is easiest to see with simple binary operators, but it is not limited to them. For example, <hask>foldr1 (+) [1,1,2,3,5]</hask> eventually creates the expression <hask>1+1+2+3+5</hask>, and thus returns 12. In the set example, <hask>foldr1 (.) (map insert myList)</hask> gives us what we want, the successive insertion of every element of ''myList.'' What is the type of this expression? It is <hask>Set -> Set</hask>, 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 />
<haskell><br />
newSet = (foldr1 (.) (map insert myList)) mySet <br />
</haskell><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