| 1 | <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Chapter 12. Příklad programů v jazyce GEL</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="index.html" title="Příručka k aplikaci Genius"><link rel="prev" href="ch11s20.html" title="Vykreslování"><link rel="next" href="ch13.html" title="Chapter 13. Nastavení"></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">Chapter 12. Příklad programů v jazyce GEL</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch11s20.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ch13.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="genius-gel-example-programs"></a>Chapter 12. Příklad programů v jazyce GEL</h1></div></div></div><p lang="en"> |
| 2 | Here is a function that calculates factorials: |
| 3 | </p><pre lang="en" class="programlisting">function f(x) = if x <= 1 then 1 else (f(x-1)*x) |
| 4 | </pre><p lang="en"> |
| 5 | </p><p lang="en"> |
| 6 | With indentation it becomes: |
| 7 | </p><pre lang="en" class="programlisting">function f(x) = ( |
| 8 | if x <= 1 then |
| 9 | 1 |
| 10 | else |
| 11 | (f(x-1)*x) |
| 12 | ) |
| 13 | </pre><p lang="en"> |
| 14 | </p><p lang="en"> |
| 15 | This is a direct port of the factorial function from the <span class="application">bc</span> manpage. The syntax seems similar to <span class="application">bc</span>, but different in that in GEL, the last expression is the one that is returned. Using the <code class="literal">return</code> function instead, it would be: |
| 16 | </p><pre lang="en" class="programlisting">function f(x) = ( |
| 17 | if (x <= 1) then return (1); |
| 18 | return (f(x-1) * x) |
| 19 | ) |
| 20 | </pre><p lang="en"> |
| 21 | </p><p lang="en"> |
| 22 | By far the easiest way to define a factorial function would be using |
| 23 | the product loop as follows. This is not only the shortest and fastest, |
| 24 | but also probably the most readable version. |
| 25 | </p><pre lang="en" class="programlisting">function f(x) = prod k=1 to x do k |
| 26 | </pre><p lang="en"> |
| 27 | </p><p lang="en"> |
| 28 | Here is a larger example, this basically redefines the internal |
| 29 | <a class="link" href="ch11s09.html#gel-function-ref"><code class="function">ref</code></a> function to calculate the row echelon form of a |
| 30 | matrix. The function <code class="function">ref</code> is built in and much faster, |
| 31 | but this example demonstrates some of the more complex features of GEL. |
| 32 | </p><pre lang="en" class="programlisting"># Calculate the row-echelon form of a matrix |
| 33 | function MyOwnREF(m) = ( |
| 34 | if not IsMatrix(m) or not IsValueOnly(m) then |
| 35 | (error("MyOwnREF: argument not a value only matrix");bailout); |
| 36 | s := min(rows(m), columns(m)); |
| 37 | i := 1; |
| 38 | d := 1; |
| 39 | while d <= s and i <= columns(m) do ( |
| 40 | |
| 41 | # This just makes the anchor element non-zero if at |
| 42 | # all possible |
| 43 | if m@(d,i) == 0 then ( |
| 44 | j := d+1; |
| 45 | while j <= rows(m) do ( |
| 46 | if m@(j,i) == 0 then |
| 47 | (j=j+1;continue); |
| 48 | a := m@(j,); |
| 49 | m@(j,) := m@(d,); |
| 50 | m@(d,) := a; |
| 51 | j := j+1; |
| 52 | break |
| 53 | ) |
| 54 | ); |
| 55 | if m@(d,i) == 0 then |
| 56 | (i:=i+1;continue); |
| 57 | |
| 58 | # Here comes the actual zeroing of all but the anchor |
| 59 | # element rows |
| 60 | j := d+1; |
| 61 | while j <= rows(m)) do ( |
| 62 | if m@(j,i) != 0 then ( |
| 63 | m@(j,) := m@(j,)-(m@(j,i)/m@(d,i))*m@(d,) |
| 64 | ); |
| 65 | j := j+1 |
| 66 | ); |
| 67 | m@(d,) := m@(d,) * (1/m@(d,i)); |
| 68 | d := d+1; |
| 69 | i := i+1 |
| 70 | ); |
| 71 | m |
| 72 | ) |
| 73 | </pre><p lang="en"> |
| 74 | </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch11s20.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ch13.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Vykreslování </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 13. Nastavení</td></tr></table></div></body></html> |