1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>10.3. Lists, Lists And More Lists</title>
<link rel="stylesheet" href="gimp-help-plain.css" type="text/css" />
<link rel="stylesheet" href="gimp-help-screen.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.66.1" />
<link rel="start" href="index.html" title=" " />
<link rel="up" href="ch02s10.html" title="10. A Script-Fu Tutorial" />
<link rel="prev" href="ch02s10s02.html" title="10.2. Variables And Functions" />
<link rel="next" href="ch02s10s04.html" title="10.4. Your First Script-Fu Script" />
</head>
<body>
<div xmlns="" class="navheader">
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center" id="chaptername">10.3. Lists, Lists And More Lists</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="ch02s10s02.html">Prev</a> </td>
<th width="60%" align="center" id="sectionname">10.3. Lists, Lists And More Lists</th>
<td width="20%" align="right"> <a accesskey="n" href="ch02s10s04.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="id3308199"></a>10.3. Lists, Lists And More Lists</h3>
</div>
</div>
</div>
<p>
We've trained you in variables and functions, and now enter the
murky swamps of Scheme's lists.
</p>
<div class="simplesect" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="id3316793"></a>Defining A List</h4>
</div>
</div>
</div>
<p>
Before we talk more about lists, it is necessary that you know
the difference between atomic values and lists.
</p>
<p>
You've already seen atomic values when we initialized
variables in the previous lesson. An atomic value is a single
value. So, for example, we can assign the variable "x" the
single value of 8 in the following statement:
</p>
<pre class="programlisting">
(let* ( (x 8) ) x)
</pre>
<p>
(We added the expression <tt class="varname">x</tt> at the end to print out the value
assigned to <tt class="varname">x</tt>-- normally you won't need to do this. Notice how
<tt class="code">let*</tt> operates just like a function: The value of
the last statement is the value returned.)
</p>
<p>
A variable may also refer to a list of values, rather than a
single value. To assign the variable <tt class="varname">x</tt> the
list of values 1, 3, 5, we'd type:
</p>
<pre class="programlisting">
(let* ( (x '(1 3 5))) x)
</pre>
<p>
Try typing both statements into the Script-Fu Console and
notice how it replies. When you type the first statement in,
it simply replies with the result:
</p>
<pre class="programlisting">
8
</pre>
<p>
However, when you type in the other statement, it replies with
the following result:
</p>
<pre class="programlisting">
(1 3 5)
</pre>
<p>
When it replies with the value 8 it is informing you that
<tt class="varname">x</tt> contains the atomic value 8. However,
when it replies with (1 3 5), it is then informing you that
<tt class="varname">x</tt> contains not a single value, but a list
of values. Notice that there are no commas in our declaration
or assignment of the list, nor in the printed result.
</p>
<p>
The syntax to define a list is:
</p>
<pre class="programlisting">
'(a b c)
</pre>
<p>
where <tt class="varname">a</tt>, <tt class="varname">b</tt>, and
<tt class="varname">c</tt> are literals. We use the apostrophe (')
to indicate that what follows in the parentheses is a list of
literal values, rather than a function or expression.
</p>
<p>
An empty list can be defined as such:
</p>
<pre class="programlisting">
'()
</pre>
<p>
or simply:
</p>
<pre class="programlisting">
()
</pre>
<p>
Lists can contain atomic values, as well as other lists:
</p>
<pre class="programlisting">
(let*
(
(x
'("The Gimp" (1 2 3) ("is" ("great" () ) ) )
)
)
x
)
</pre>
<p>
Notice that after the first apostrophe, you no longer need to
use an apostrophe when defining the inner lists. Go ahead and
copy the statement into the Script-Fu Console and see what it
returns.
</p>
<p>
You should notice that the result returned is not a list of
single, atomic values; rather, it is a list of a literal <tt class="code">("The
Gimp")</tt>, the list <tt class="code">(1 2 3)</tt>, etc.
</p>
</div>
<div class="simplesect" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="id3316666"></a>How To Think Of Lists</h4>
</div>
</div>
</div>
<p>
It's useful to think of lists as composed of a "head" and a
"tail." The head is the first element of the list, the tail
the rest of the list. You'll see why this is important when we
discuss how to add to lists and how to access elements in the
list.
</p>
</div>
<div class="simplesect" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="id3317060"></a>Creating Lists Through Concatenation (The Cons Function)</h4>
</div>
</div>
</div>
<p>
One of the more common functions you'll encounter is the cons
function. It takes a value and prepends it to its second
argument, a list. From the previous section, I suggested that
you think of a list as being composed of an element (the head)
and the remainder of the list (the tail). This is exactly how
cons functions -- it adds an element to the head of a
list. Thus, you could create a list as follows:
</p>
<pre class="programlisting">
(cons 1 '(2 3 4) )
</pre>
<p>
The result is the list <tt class="code">(1 2 3 4)</tt>.
</p>
<p>
You could also create a list with one element:
</p>
<pre class="programlisting">
(cons 1 () )
</pre>
<p>
You can use previously declared variables in place of any
literals, as you would expect.
</p>
</div>
<div class="simplesect" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="id3316697"></a>Defining A List Using The list Function</h4>
</div>
</div>
</div>
<p>
To define a list composed of literals or previously declared
variables, use the list function:
</p>
<pre class="programlisting">
(list 5 4 3 a b c)
</pre>
<p>
This will compose and return a list containing the values held
by the variables <tt class="varname">a</tt>, <tt class="varname">b</tt>
and <tt class="varname">c</tt>. For example:
</p>
<pre class="programlisting">
(let* (
(a 1)
(b 2)
(c 3)
)
(list 5 4 3 a b c)
)
</pre>
<p>
This code creates the list <tt class="code">(5 4 3 1 2 3)</tt>.
</p>
</div>
<div class="simplesect" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="id3317463"></a>Accessing Values In A List</h4>
</div>
</div>
</div>
<p>
To access the values in a list, use the functions <tt class="code">car</tt> and <tt class="code">cdr</tt>,
which return the first element of the list and the rest of the
list, respectively. These functions break the list down into
the head::tail construct I mentioned earlier.
</p>
</div>
<div class="simplesect" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="id3317501"></a>The <tt class="code">car</tt> Function</h4>
</div>
</div>
</div>
<p>
<tt class="code">car</tt> returns the first element of the list (the
head of the list). The list needs to be non-null. Thus, the
following returns the first element of the list:
</p>
<pre class="programlisting">
(car '("first" 2 "third"))
</pre>
<p>
which is:
</p>
<pre class="programlisting">
"first"
</pre>
</div>
<div class="simplesect" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="id3317352"></a>The <tt class="code">cdr</tt> function</h4>
</div>
</div>
</div>
<p>
<tt class="code">cdr</tt> returns the rest of the list after the first
element (the tail of the list). If there is only one element
in the list, it returns an empty list.
</p>
<pre class="programlisting">
(cdr '("first" 2 "third"))
</pre>
<p>
returns:
</p>
<pre class="programlisting">
(2 "third")
</pre>
<p>
whereas the following:
</p>
<pre class="programlisting">
(cdr '("one and only"))
</pre>
<p>
returns:
</p>
<pre class="programlisting">
()
</pre>
</div>
<div class="simplesect" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="id3317554"></a>Accessing Other Elements In A List</h4>
</div>
</div>
</div>
<p>
OK, great, we can get the first element in a list, as well as
the rest of the list, but how do we access the second, third
or other elements of a list? There exist several "convenience"
functions to access, for example, the head of the head of the
tail of a list (<tt class="code">caadr</tt>), the tail of the tail of a
list (<tt class="code">cddr</tt>), etc.
</p>
<p>
The basic naming convention is easy: The a's and d's represent
the heads and tails of lists, so
</p>
<pre class="programlisting">
(car (cdr (car x) ) )
</pre>
<p>
could be written as:
</p>
<pre class="programlisting">
(cadar x)
</pre>
<p>
To view a full list of the list functions, refer to the
Appendix, which lists the available functions for the version
of Scheme used by Script-Fu.
</p>
<p>
To get some practice with list-accessing functions, try typing
in the following (except all on one line if you're using the
console); use different variations of car and cdr to access
the different elements of the list:
</p>
<pre class="programlisting">
(let* (
(x '( (1 2 (3 4 5) 6) 7 8 (9 10) )
)
)
; place your car/cdr code here
)
</pre>
<p>
Try accessing the number 3 in the list using only two function
calls. If you can do that, you're on your way to becoming a
Script-Fu Master!
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25">
<img alt="[Note]" src="../images/note.png" />
</td>
<th align="left">Note</th>
</tr>
<tr>
<td colspan="2" align="left" valign="top">
<p>
In Scheme, a semicolon (";") marks a comment. It, and
anything that follows it on the same line, are ignored by the
script interpreter, so you can use this to add comments to jog
your memory when you look at the script later.
</p>
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="ch02s10s02.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="ch02s10.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="ch02s10s04.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">10.2. Variables And Functions </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> 10.4. Your First Script-Fu Script</td>
</tr>
</table>
</div>
</body>
</html>
|