1 | <html> |
---|
2 | <head> |
---|
3 | <!-- Generated by the Spirit (http://spirit.sf.net) QuickDoc --> |
---|
4 | <title>Lazy functions</title> |
---|
5 | <link rel="stylesheet" href="theme/style.css" type="text/css"> |
---|
6 | <link rel="prev" href="architecture.html"> |
---|
7 | <link rel="next" href="place_holders.html"> |
---|
8 | </head> |
---|
9 | <body> |
---|
10 | <table width="100%" height="48" border="0" background="theme/bkd2.gif" cellspacing="2"> |
---|
11 | <tr> |
---|
12 | <td width="10"> |
---|
13 | </td> |
---|
14 | <td width="85%"> |
---|
15 | <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>Lazy functions</b></font> |
---|
16 | </td> |
---|
17 | <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" align="right" border="0"></a></td> |
---|
18 | </tr> |
---|
19 | </table> |
---|
20 | <br> |
---|
21 | <table border="0"> |
---|
22 | <tr> |
---|
23 | <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td> |
---|
24 | <td width="30"><a href="architecture.html"><img src="theme/l_arr.gif" border="0"></a></td> |
---|
25 | <td width="20"><a href="place_holders.html"><img src="theme/r_arr.gif" border="0"></a></td> |
---|
26 | </tr> |
---|
27 | </table> |
---|
28 | <p> |
---|
29 | When currying or partial function evaluation takes place, supplying N actual arguments to a function that expects M arguments where N < M will result in a higher order function with M-N arguments. Technically, when N == M, the function has all the arguments needed to do a full evaluation:</p> |
---|
30 | <code><pre> |
---|
31 | <span class=identifier>plus</span><span class=special>(</span><span class=number>3</span><span class=special>, </span><span class=number>2</span><span class=special>) </span><span class=identifier>full </span><span class=identifier>evaluation |
---|
32 | </span><span class=identifier>plus</span><span class=special>(?, </span><span class=number>6</span><span class=special>) </span><span class=identifier>partial </span><span class=identifier>evaluation |
---|
33 | </span></pre></code> |
---|
34 | <table width="80%" border="0" align="center"> |
---|
35 | <tr> |
---|
36 | <td class="note_box"> |
---|
37 | <img src="theme/note.gif"></img> Lazy functions are subsets of partial function evaluation or currying </td> |
---|
38 | </tr> |
---|
39 | </table> |
---|
40 | <p> |
---|
41 | Now, we shall revisit the concept of lazy functions introduced before in passing. That is, the first function invocation will not really "fully evaluate" the function regardless if all or some of the arguments are supplied. A second function invocation will always be needed to perform the actual evaluation. At the point in the second call, the caller is expected to supply all arguments that are still missing. Still vague? To clarify, a partially evaluated function:</p> |
---|
42 | <code><pre> |
---|
43 | <span class=identifier>f</span><span class=special>(</span><span class=number>1</span><span class=special>, ?, ?) |
---|
44 | </span></pre></code> |
---|
45 | <p> |
---|
46 | results to an unnamed function unnamed_f(a, b) that expects the two (2) more arguments that are still missing when the first function, f, is invoked. Since unnamed_f(a, b) is already a second level evaluation, all arguments must be supplied when it is called and the result is finally evaluated. Example:</p> |
---|
47 | <code><pre> |
---|
48 | <span class=identifier>f</span><span class=special>(</span><span class=number>1</span><span class=special>, ?, ?) ---> </span><span class=identifier>unnamed_f</span><span class=special>(</span><span class=identifier>a</span><span class=special>, </span><span class=identifier>b</span><span class=special>) |
---|
49 | </span></pre></code> |
---|
50 | <p> |
---|
51 | then</p> |
---|
52 | <code><pre> |
---|
53 | <span class=identifier>unnamed_f</span><span class=special>(</span><span class=number>2</span><span class=special>, </span><span class=number>3</span><span class=special>) ---> </span><span class=identifier>evaluate_and_return_value_for </span><span class=identifier>f</span><span class=special>(</span><span class=number>1</span><span class=special>, </span><span class=number>2</span><span class=special>, </span><span class=number>3</span><span class=special>) |
---|
54 | </span></pre></code> |
---|
55 | <p> |
---|
56 | This function call sequence can be concatenated:</p> |
---|
57 | <code><pre> |
---|
58 | <span class=identifier>f</span><span class=special>(</span><span class=number>1</span><span class=special>, ?, ?)(</span><span class=number>2</span><span class=special>, </span><span class=number>3</span><span class=special>) |
---|
59 | </span></pre></code> |
---|
60 | <p> |
---|
61 | The second level function unnamed_f is not curryable. All of its still missing arguments must be supplied when it is called.</p> |
---|
62 | <p> |
---|
63 | As mentioned, even though we have all the arguments in the first call, the function is not yet evaluated (thus lazy). For example, a function call:</p> |
---|
64 | <code><pre> |
---|
65 | <span class=identifier>f</span><span class=special>(</span><span class=number>1</span><span class=special>, </span><span class=number>2</span><span class=special>, </span><span class=number>3</span><span class=special>) |
---|
66 | </span></pre></code> |
---|
67 | <p> |
---|
68 | remember that the first function invocation will not really evaluate the function even if all the arguments are fully supplied. Thus:</p> |
---|
69 | <code><pre> |
---|
70 | <span class=identifier>f</span><span class=special>(</span><span class=number>1</span><span class=special>, </span><span class=number>2</span><span class=special>, </span><span class=number>3</span><span class=special>) ---> </span><span class=identifier>unnamed_f</span><span class=special>() |
---|
71 | </span></pre></code> |
---|
72 | <table width="80%" border="0" align="center"> |
---|
73 | <tr> |
---|
74 | <td class="note_box"> |
---|
75 | <img src="theme/note.gif"></img> <b>Generators</b><br><br>In FP, unnamed_f() is a generator; a function that has no arguments but returns a result. Not to be confused with Phoenix generators to be discussed later. </td> |
---|
76 | </tr> |
---|
77 | </table> |
---|
78 | <p> |
---|
79 | Then:</p> |
---|
80 | <code><pre> |
---|
81 | <span class=identifier>unnamed_f</span><span class=special>() ---> </span><span class=identifier>evaluate_and_return_value_for </span><span class=identifier>f</span><span class=special>(</span><span class=number>1</span><span class=special>, </span><span class=number>2</span><span class=special>, </span><span class=number>3</span><span class=special>) |
---|
82 | </span></pre></code> |
---|
83 | <p> |
---|
84 | This function call sequence can be concatenated as:</p> |
---|
85 | <code><pre> |
---|
86 | <span class=identifier>f</span><span class=special>(</span><span class=number>1</span><span class=special>, </span><span class=number>2</span><span class=special>, </span><span class=number>3</span><span class=special>)() |
---|
87 | </span></pre></code> |
---|
88 | <p> |
---|
89 | Lambda (unnamed) functions and currying (partial function evaluation) can also be applied to operators. However, unlike lazy functions, operators are fully evaluated once all the arguments are known and supplied, unless some sort of intervention is applied to coerce the operator expression to be lazily evaluated. We shall see more details on this and how this is done later.</p> |
---|
90 | <table border="0"> |
---|
91 | <tr> |
---|
92 | <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td> |
---|
93 | <td width="30"><a href="architecture.html"><img src="theme/l_arr.gif" border="0"></a></td> |
---|
94 | <td width="20"><a href="place_holders.html"><img src="theme/r_arr.gif" border="0"></a></td> |
---|
95 | </tr> |
---|
96 | </table> |
---|
97 | <br> |
---|
98 | <hr size="1"> |
---|
99 | <p class="copyright">Copyright © 2001-2002 Joel de Guzman<br> |
---|
100 | <br> |
---|
101 | <font size="2">Use, modification and distribution is subject to the Boost Software |
---|
102 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
103 | http://www.boost.org/LICENSE_1_0.txt) </font> </p> |
---|
104 | </body> |
---|
105 | </html> |
---|