|
| 1 | +.. _functions: |
| 2 | + |
| 3 | +Functions |
| 4 | +========= |
| 5 | + |
| 6 | + |
| 7 | +.. _lambdas: |
| 8 | + |
| 9 | +Function literals |
| 10 | +----------------- |
| 11 | + |
| 12 | +Function literals in Haskell are also often called **lambda functions**. |
| 13 | +The syntax is a slash ``\`` followed by a list of space separated paramters, follwed by an ASCII arrow ``->`` upon which follows the body of the function. |
| 14 | +Function bodies in Haskell are always an expression, and as such require no ``return`` keyword. |
| 15 | + |
| 16 | +:: |
| 17 | + |
| 18 | + \ param -> param |
| 19 | + |
| 20 | +Here for instance we have a function which takes one parameter as input and return it. |
| 21 | +This function is also known as ``id``. |
| 22 | + |
| 23 | +:: |
| 24 | + |
| 25 | + -- we often call an unspecified parameter 'x' |
| 26 | + let id = \x -> x |
| 27 | + |
| 28 | +Haskell is a functional language. |
| 29 | +As such functions may be used just like any other value including being assigned to bindings. |
| 30 | +The type of our binding is now the function type. |
| 31 | + |
| 32 | +:: |
| 33 | + |
| 34 | + let id :: a -> a |
| 35 | + id = \x -> x |
| 36 | + |
| 37 | +When we have a value of the function type we may apply it to an argument of the type *left* of the arrow to obtain a value of the type *right* of the arrow. |
| 38 | +Ergo ``Int -> Bool`` applied to ``Int`` gives a ``Bool``. |
| 39 | +Similarly ``a -> a`` applied to ``Int`` gives an ``Int`` again. |
| 40 | +An ``a -> a`` applied to a ``Bool`` gives a ``Bool``. |
| 41 | + |
| 42 | +To apply a function we ise the simplest synta of all, juxtaposition. |
| 43 | +Also called *postfix notation* or "the function followed by the arguments, all space separated". |
| 44 | + |
| 45 | +:: |
| 46 | + |
| 47 | + let id :: a -> a |
| 48 | + id = \x -> x |
| 49 | + |
| 50 | + myBool = id True |
| 51 | + myBool2 = (\x -> x) True |
| 52 | + myInt = id 5 |
| 53 | + |
| 54 | + myBool == myBool2 == True && myInt == 5 |
| 55 | + |
| 56 | +Lets look at another example fuction: |
| 57 | + |
| 58 | +:: |
| 59 | + |
| 60 | + let const :: a -> b -> a |
| 61 | + const = \x _ -> x |
| 62 | + |
| 63 | +The ``const`` function takes a first parameter ``x`` and a second parameter, which we ignore. |
| 64 | +The underscore ``_`` as a parameter or binding name is used to indicate that we ignore the value. |
| 65 | +And finally the function returns the first parameter. |
| 66 | + |
| 67 | +Note that the type of the function is now ``a -> b -> a``. |
| 68 | +We see here that the function type ``->`` occurs twice and this is deliberate because we may rewrite our function as follows: |
| 69 | + |
| 70 | +:: |
| 71 | + |
| 72 | + let const :: a -> (b -> a) |
| 73 | + const = \x -> \_ -> x |
| 74 | + |
| 75 | +Now we can see the analogy. |
| 76 | +We first consume the first parameter and return a function. |
| 77 | +This second function is then applied to the second parameter returning the final value. |
| 78 | +The two versions ``\x _ -> x`` and ``\x -> \_ -> x`` and their type signatures are equivalent in Haskell, hence the same type. |
| 79 | + |
| 80 | +The practical upshot of this is that haskell makes it extremely easy to do what is often called "partially applied functions". |
| 81 | +This means supplying fewer arguments to a function than would be required to produce its final value. |
| 82 | +Technically this is not even possible in Haskell, since, as we have seen above, every Haskell function only takes one argument but may return a curried function to simulate being given a second argument. |
| 83 | +To fully grasp the possibilities that partial application offers it is instrumental to internalise this aforementioned concept. |
| 84 | + |
| 85 | +Partial application is best described using examples: |
| 86 | + |
| 87 | +:: |
| 88 | + |
| 89 | + let const :: a -> b -> a |
| 90 | + const x _ = x |
| 91 | + |
| 92 | + alwaysFive = const 5 |
| 93 | + |
| 94 | + alwaysFive "a string" == alwaysFive 6 == alwaysFive () == 5 |
| 95 | + |
| 96 | + let plusThree = (+ 3) |
| 97 | + |
| 98 | + plusThree 5 == 8 |
| 99 | + plusThree 10 == 13 |
| 100 | + |
| 101 | +.. admonition:: Aside |
| 102 | + |
| 103 | + This is particularly useful when combined with :ref:`higher order functions`. |
| 104 | + |
| 105 | + For instance we can increment a whole list of integers using the partial application of ``+`` to ``1``. |
| 106 | + |
| 107 | + :: |
| 108 | + |
| 109 | + map (+ 1) [4,5,8] == [5,6,9] |
| 110 | + |
| 111 | + Or to find the index of a particular element in a list: (partial application of ``==``) |
| 112 | + |
| 113 | + :: |
| 114 | + |
| 115 | + find (== 6) [3,6,8] == Just 2 |
| 116 | + |
| 117 | + Note that these are advanced examples, there is no need to undestand them yet, we will cover those in detail later. |
| 118 | + |
| 119 | + |
| 120 | +Syntactic sugar for function definitions |
| 121 | +---------------------------------------- |
| 122 | + |
| 123 | +There are a few common patterns in Haskell when defining functions. |
| 124 | +The first is for creating function values. |
| 125 | + |
| 126 | +:: |
| 127 | + |
| 128 | + myFunction = \a b -> doSomthing |
| 129 | + |
| 130 | + let anotherFunction = \x -> expr |
| 131 | + |
| 132 | +This pattern is very common. |
| 133 | +Therefore we have some syntactic sugar in the Haskell laguage which allows us to omit both the backslash ``\`` and the arrow ``->`` by moving the function arguments before the equal sign. |
| 134 | + |
| 135 | +:: |
| 136 | + |
| 137 | + myFunction a b = doSomthing |
| 138 | + |
| 139 | + let anotherFunction x = expr |
| 140 | + |
| 141 | +Another common pattern is to take arguments to a function and immediately perform a ``case`` match on them. |
| 142 | + |
| 143 | +:: |
| 144 | + |
| 145 | + myFunction a = |
| 146 | + case a of |
| 147 | + Constr1 val -> ... |
| 148 | + Constr2 v2 -> ... |
| 149 | + |
| 150 | +Instead we may write |
| 151 | + |
| 152 | +:: |
| 153 | + |
| 154 | + myFunction (Constr1 val) = ... |
| 155 | + myFunction (Constr2 v2) = ... |
| 156 | + |
| 157 | +.. note:: |
| 158 | + Here we must use parentheses around the match clauses to distinguish the clauses for several arguments. |
| 159 | + |
0 commit comments