Debian Codegraph @

HTML/recurnolocvar.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Recursion Without Local Variables</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
9 "><LINK
10 REL="HOME"
11 TITLE="Advanced Bash-Scripting Guide"
12 HREF="index.html"><LINK
13 REL="UP"
14 TITLE="Functions"
15 HREF="functions.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Local Variables"
18 HREF="localvar.html"><LINK
19 REL="NEXT"
20 TITLE="Aliases"
21 HREF="aliases.html"><META
22 HTTP-EQUIV="Content-Style-Type"
23 CONTENT="text/css"><LINK
24 REL="stylesheet"
25 HREF="common/kde-common.css"
26 TYPE="text/css"><META
27 HTTP-EQUIV="Content-Type"
28 CONTENT="text/html; charset=iso-8859-1"><META
29 HTTP-EQUIV="Content-Language"
30 CONTENT="en"><LINK
31 REL="stylesheet"
32 HREF="common/kde-localised.css"
33 TYPE="text/css"
34 TITLE="KDE-English"><LINK
35 REL="stylesheet"
36 HREF="common/kde-default.css"
37 TYPE="text/css"
38 TITLE="KDE-Default"></HEAD
39 ><BODY
40 CLASS="SECT1"
41 BGCOLOR="#FFFFFF"
42 TEXT="#000000"
43 LINK="#AA0000"
44 VLINK="#AA0055"
45 ALINK="#AA0000"
46 STYLE="font-family: sans-serif;"
47 ><DIV
48 CLASS="NAVHEADER"
49 ><TABLE
50 SUMMARY="Header navigation table"
51 WIDTH="100%"
52 BORDER="0"
53 CELLPADDING="0"
54 CELLSPACING="0"
55 ><TR
56 ><TH
57 COLSPAN="3"
58 ALIGN="center"
59 >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
60 ></TR
61 ><TR
62 ><TD
63 WIDTH="10%"
64 ALIGN="left"
65 VALIGN="bottom"
66 ><A
67 HREF="localvar.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 >Chapter 24. Functions</TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="aliases.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="SECT1"
92 ><H1
93 CLASS="SECT1"
94 ><A
95 NAME="RECURNOLOCVAR"
96 ></A
97 >24.3. Recursion Without Local Variables</H1
98 ><P
99 >A function may recursively call itself even without use of
100 local variables.</P
101 ><P
102 ><A
103 NAME="FIBOREF"
104 ></A
105 ></P
106 ><DIV
107 CLASS="EXAMPLE"
108 ><HR><A
109 NAME="FIBO"
110 ></A
111 ><P
112 ><B
113 >Example 24-16. <I
114 CLASS="FIRSTTERM"
115 >The Fibonacci Sequence</I
116 ></B
117 ></P
118 ><TABLE
119 BORDER="0"
120 BGCOLOR="#E0E0E0"
121 WIDTH="100%"
122 ><TR
123 ><TD
124 ><PRE
125 CLASS="PROGRAMLISTING"
126 > 1&nbsp;#!/bin/bash
127 2&nbsp;# fibo.sh : Fibonacci sequence (recursive)
128 3&nbsp;# Author: M. Cooper
129 4&nbsp;# License: GPL3
130 5&nbsp;
131 6&nbsp;# ----------algorithm--------------
132 7&nbsp;# Fibo(0) = 0
133 8&nbsp;# Fibo(1) = 1
134 9&nbsp;# else
135 10&nbsp;# Fibo(j) = Fibo(j-1) + Fibo(j-2)
136 11&nbsp;# ---------------------------------
137 12&nbsp;
138 13&nbsp;MAXTERM=15 # Number of terms (+1) to generate.
139 14&nbsp;MINIDX=2 # If idx is less than 2, then Fibo(idx) = idx.
140 15&nbsp;
141 16&nbsp;Fibonacci ()
142 17&nbsp;{
143 18&nbsp; idx=$1 # Doesn't need to be local. Why not?
144 19&nbsp; if [ "$idx" -lt "$MINIDX" ]
145 20&nbsp; then
146 21&nbsp; echo "$idx" # First two terms are 0 1 ... see above.
147 22&nbsp; else
148 23&nbsp; (( --idx )) # j-1
149 24&nbsp; term1=$( Fibonacci $idx ) # Fibo(j-1)
150 25&nbsp;
151 26&nbsp; (( --idx )) # j-2
152 27&nbsp; term2=$( Fibonacci $idx ) # Fibo(j-2)
153 28&nbsp;
154 29&nbsp; echo $(( term1 + term2 ))
155 30&nbsp; fi
156 31&nbsp; # An ugly, ugly kludge.
157 32&nbsp; # The more elegant implementation of recursive fibo in C
158 33&nbsp; #+ is a straightforward translation of the algorithm in lines 7 - 10.
159 34&nbsp;}
160 35&nbsp;
161 36&nbsp;for i in $(seq 0 $MAXTERM)
162 37&nbsp;do # Calculate $MAXTERM+1 terms.
163 38&nbsp; FIBO=$(Fibonacci $i)
164 39&nbsp; echo -n "$FIBO "
165 40&nbsp;done
166 41&nbsp;# 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
167 42&nbsp;# Takes a while, doesn't it? Recursion in a script is slow.
168 43&nbsp;
169 44&nbsp;echo
170 45&nbsp;
171 46&nbsp;exit 0</PRE
172 ></TD
173 ></TR
174 ></TABLE
175 ><HR></DIV
176 ><P
177 ><A
178 NAME="HANOIREF"
179 ></A
180 ></P
181 ><DIV
182 CLASS="EXAMPLE"
183 ><HR><A
184 NAME="HANOI"
185 ></A
186 ><P
187 ><B
188 >Example 24-17. <I
189 CLASS="FIRSTTERM"
190 >The Towers of Hanoi</I
191 ></B
192 ></P
193 ><TABLE
194 BORDER="0"
195 BGCOLOR="#E0E0E0"
196 WIDTH="100%"
197 ><TR
198 ><TD
199 ><PRE
200 CLASS="PROGRAMLISTING"
201 > 1&nbsp;#! /bin/bash
202 2&nbsp;#
203 3&nbsp;# The Towers Of Hanoi
204 4&nbsp;# Bash script
205 5&nbsp;# Copyright (C) 2000 Amit Singh. All Rights Reserved.
206 6&nbsp;# http://hanoi.kernelthread.com
207 7&nbsp;#
208 8&nbsp;# Tested under Bash version 2.05b.0(13)-release.
209 9&nbsp;# Also works under Bash version 3.x.
210 10&nbsp;#
211 11&nbsp;# Used in "Advanced Bash Scripting Guide"
212 12&nbsp;#+ with permission of script author.
213 13&nbsp;# Slightly modified and commented by ABS author.
214 14&nbsp;
215 15&nbsp;#=================================================================#
216 16&nbsp;# The Tower of Hanoi is a mathematical puzzle attributed to
217 17&nbsp;#+ Edouard Lucas, a nineteenth-century French mathematician.
218 18&nbsp;#
219 19&nbsp;# There are three vertical posts set in a base.
220 20&nbsp;# The first post has a set of annular rings stacked on it.
221 21&nbsp;# These rings are disks with a hole drilled out of the center,
222 22&nbsp;#+ so they can slip over the posts and rest flat.
223 23&nbsp;# The rings have different diameters, and they stack in ascending
224 24&nbsp;#+ order, according to size.
225 25&nbsp;# The smallest ring is on top, and the largest on the bottom.
226 26&nbsp;#
227 27&nbsp;# The task is to transfer the stack of rings
228 28&nbsp;#+ to one of the other posts.
229 29&nbsp;# You can move only one ring at a time to another post.
230 30&nbsp;# You are permitted to move rings back to the original post.
231 31&nbsp;# You may place a smaller ring atop a larger one,
232 32&nbsp;#+ but *not* vice versa.
233 33&nbsp;# Again, it is forbidden to place a larger ring atop a smaller one.
234 34&nbsp;#
235 35&nbsp;# For a small number of rings, only a few moves are required.
236 36&nbsp;#+ For each additional ring,
237 37&nbsp;#+ the required number of moves approximately doubles,
238 38&nbsp;#+ and the "strategy" becomes increasingly complicated.
239 39&nbsp;#
240 40&nbsp;# For more information, see http://hanoi.kernelthread.com
241 41&nbsp;#+ or pp. 186-92 of _The Armchair Universe_ by A.K. Dewdney.
242 42&nbsp;#
243 43&nbsp;#
244 44&nbsp;# ... ... ...
245 45&nbsp;# | | | | | |
246 46&nbsp;# _|_|_ | | | |
247 47&nbsp;# |_____| | | | |
248 48&nbsp;# |_______| | | | |
249 49&nbsp;# |_________| | | | |
250 50&nbsp;# |___________| | | | |
251 51&nbsp;# | | | | | |
252 52&nbsp;# .--------------------------------------------------------------.
253 53&nbsp;# |**************************************************************|
254 54&nbsp;# #1 #2 #3
255 55&nbsp;#
256 56&nbsp;#=================================================================#
257 57&nbsp;
258 58&nbsp;
259 59&nbsp;E_NOPARAM=66 # No parameter passed to script.
260 60&nbsp;E_BADPARAM=67 # Illegal number of disks passed to script.
261 61&nbsp;Moves= # Global variable holding number of moves.
262 62&nbsp; # Modification to original script.
263 63&nbsp;
264 64&nbsp;dohanoi() { # Recursive function.
265 65&nbsp; case $1 in
266 66&nbsp; 0)
267 67&nbsp; ;;
268 68&nbsp; *)
269 69&nbsp; dohanoi "$(($1-1))" $2 $4 $3
270 70&nbsp; echo move $2 "--&#62;" $3
271 71&nbsp; ((Moves++)) # Modification to original script.
272 72&nbsp; dohanoi "$(($1-1))" $4 $3 $2
273 73&nbsp; ;;
274 74&nbsp; esac
275 75&nbsp;}
276 76&nbsp;
277 77&nbsp;case $# in
278 78&nbsp; 1) case $(($1&#62;0)) in # Must have at least one disk.
279 79&nbsp; 1) # Nested case statement.
280 80&nbsp; dohanoi $1 1 3 2
281 81&nbsp; echo "Total moves = $Moves" # 2^n - 1, where n = # of disks.
282 82&nbsp; exit 0;
283 83&nbsp; ;;
284 84&nbsp; *)
285 85&nbsp; echo "$0: illegal value for number of disks";
286 86&nbsp; exit $E_BADPARAM;
287 87&nbsp; ;;
288 88&nbsp; esac
289 89&nbsp; ;;
290 90&nbsp; *)
291 91&nbsp; echo "usage: $0 N"
292 92&nbsp; echo " Where \"N\" is the number of disks."
293 93&nbsp; exit $E_NOPARAM;
294 94&nbsp; ;;
295 95&nbsp;esac
296 96&nbsp;
297 97&nbsp;# Exercises:
298 98&nbsp;# ---------
299 99&nbsp;# 1) Would commands beyond this point ever be executed?
300 100&nbsp;# Why not? (Easy)
301 101&nbsp;# 2) Explain the workings of the workings of the "dohanoi" function.
302 102&nbsp;# (Difficult -- see the Dewdney reference, above.)</PRE
303 ></TD
304 ></TR
305 ></TABLE
306 ><HR></DIV
307 ></DIV
308 ><DIV
309 CLASS="NAVFOOTER"
310 ><HR
311 ALIGN="LEFT"
312 WIDTH="100%"><TABLE
313 SUMMARY="Footer navigation table"
314 WIDTH="100%"
315 BORDER="0"
316 CELLPADDING="0"
317 CELLSPACING="0"
318 ><TR
319 ><TD
320 WIDTH="33%"
321 ALIGN="left"
322 VALIGN="top"
323 ><A
324 HREF="localvar.html"
325 ACCESSKEY="P"
326 >Prev</A
327 ></TD
328 ><TD
329 WIDTH="34%"
330 ALIGN="center"
331 VALIGN="top"
332 ><A
333 HREF="index.html"
334 ACCESSKEY="H"
335 >Home</A
336 ></TD
337 ><TD
338 WIDTH="33%"
339 ALIGN="right"
340 VALIGN="top"
341 ><A
342 HREF="aliases.html"
343 ACCESSKEY="N"
344 >Next</A
345 ></TD
346 ></TR
347 ><TR
348 ><TD
349 WIDTH="33%"
350 ALIGN="left"
351 VALIGN="top"
352 >Local Variables</TD
353 ><TD
354 WIDTH="34%"
355 ALIGN="center"
356 VALIGN="top"
357 ><A
358 HREF="functions.html"
359 ACCESSKEY="U"
360 >Up</A
361 ></TD
362 ><TD
363 WIDTH="33%"
364 ALIGN="right"
365 VALIGN="top"
366 >Aliases</TD
367 ></TR
368 ></TABLE
369 ></DIV
370 ></BODY
371 ></HTML
372 >