| 1 | <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Vracení funkcí</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"><link rel="home" href="index.html" title="Příručka k aplikaci Genius"><link rel="up" href="ch07.html" title="Chapter 7. Pokročilé programování v jazyce GEL"><link rel="prev" href="ch07s02.html" title="Syntaxe v nejvyšší úrovni"><link rel="next" href="ch07s04.html" title="Skutečně lokální proměnné"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Vracení funkcí</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch07s02.html">Prev</a> </td><th width="60%" align="center">Chapter 7. Pokročilé programování v jazyce GEL</th><td width="20%" align="right"> <a accesskey="n" href="ch07s04.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="genius-gel-returning-functions"></a>Vracení funkcí</h2></div></div></div><p lang="en"> |
| 2 | It is possible to return functions as value. This way you can |
| 3 | build functions that construct special purpose functions according |
| 4 | to some parameters. The tricky bit is what variables does the |
| 5 | function see. The way this works in GEL is that when a function |
| 6 | returns another function, all identifiers referenced in the |
| 7 | function body that went out of scope |
| 8 | are prepended a private dictionary of the returned |
| 9 | function. So the function will see all variables that were in |
| 10 | scope |
| 11 | when it was defined. For example, we define a function that |
| 12 | returns a function that adds 5 to its argument. |
| 13 | </p><pre lang="en" class="programlisting">function f() = ( |
| 14 | k = 5; |
| 15 | `(x) = (x+k) |
| 16 | ) |
| 17 | </pre><p lang="en"> |
| 18 | Notice that the function adds <code class="varname">k</code> to |
| 19 | <code class="varname">x</code>. You could use this as follows. |
| 20 | </p><pre lang="en" class="programlisting">g = f(); |
| 21 | g(5) |
| 22 | </pre><p lang="en"> |
| 23 | And <strong class="userinput"><code>g(5)</code></strong> should return 10. |
| 24 | </p><p lang="en"> |
| 25 | One thing to note is that the value of <code class="varname">k</code> |
| 26 | that is used is the one that's in effect when the |
| 27 | <code class="function">f</code> returns. For example: |
| 28 | </p><pre lang="en" class="programlisting">function f() = ( |
| 29 | k := 5; |
| 30 | function r(x) = (x+k); |
| 31 | k := 10; |
| 32 | r |
| 33 | ) |
| 34 | </pre><p lang="en"> |
| 35 | will return a function that adds 10 to its argument rather than |
| 36 | 5. This is because the extra dictionary is created only when |
| 37 | the context |
| 38 | in which the function was defined ends, which is when the function |
| 39 | <code class="function">f</code> returns. This is consistent with how you |
| 40 | would expect the function <code class="function">r</code> to work inside |
| 41 | the function <code class="function">f</code> according to the rules of |
| 42 | scope of variables in GEL. Only those variables are added to the |
| 43 | extra dictionary that are in the context that just ended and |
| 44 | no longer exists. Variables |
| 45 | used in the function that are in still valid contexts will work |
| 46 | as usual, using the current value of the variable. |
| 47 | The only difference is with global variables and functions. |
| 48 | All identifiers that referenced global variables at time of |
| 49 | the function definition are not added to the private dictionary. |
| 50 | This is to avoid much unnecessary work when returning functions |
| 51 | and would rarely be a problem. For example, suppose that you |
| 52 | delete the "k=5" from the function <code class="function">f</code>, |
| 53 | and at the top level you define <code class="varname">k</code> to be |
| 54 | say 5. Then when you run <code class="function">f</code>, the function |
| 55 | <code class="function">r</code> will not put <code class="varname">k</code> into |
| 56 | the private dictionary because it was global (toplevel) |
| 57 | at the time of definition of <code class="function">r</code>. |
| 58 | </p><p lang="en"> |
| 59 | Sometimes it is better to have more control over how variables |
| 60 | are copied into the private dictionary. Since version 1.0.7, |
| 61 | you can specify which |
| 62 | variables are copied into the private dictionary by putting |
| 63 | extra square brackets after the arguments with the list of |
| 64 | variables to be copied separated by commas. |
| 65 | If you do this, then variables are |
| 66 | copied into the private dictionary at time of the function |
| 67 | definition, and the private dictionary is not touched afterwards. |
| 68 | For example |
| 69 | </p><pre lang="en" class="programlisting">function f() = ( |
| 70 | k := 5; |
| 71 | function r(x) [k] = (x+k); |
| 72 | k := 10; |
| 73 | r |
| 74 | ) |
| 75 | </pre><p lang="en"> |
| 76 | will return a function that when called will add 5 to its |
| 77 | argument. The local copy of <code class="varname">k</code> was created |
| 78 | when the function was defined. |
| 79 | </p><p lang="en"> |
| 80 | When you want the function to not have any private dictionary |
| 81 | then put empty square brackets after the argument list. Then |
| 82 | no private dictionary will be created at all. Doing this is |
| 83 | good to increase efficiency when a private dictionary is not |
| 84 | needed or when you want the function to lookup all variables |
| 85 | as it sees them when called. For example suppose you want |
| 86 | the function returned from <code class="function">f</code> to see |
| 87 | the value of <code class="varname">k</code> from the toplevel despite |
| 88 | there being a local variable of the same name during definition. |
| 89 | So the code |
| 90 | </p><pre lang="en" class="programlisting">function f() = ( |
| 91 | k := 5; |
| 92 | function r(x) [] = (x+k); |
| 93 | r |
| 94 | ); |
| 95 | k := 10; |
| 96 | g = f(); |
| 97 | g(10) |
| 98 | </pre><p lang="en"> |
| 99 | will return 20 and not 15, which would happen if |
| 100 | <code class="varname">k</code> with a value of 5 was added to the private |
| 101 | dictionary. |
| 102 | </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch07s02.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch07.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch07s04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Syntaxe v nejvyšší úrovni </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Skutečně lokální proměnné</td></tr></table></div></body></html> |