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 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html >
<head><title>Operators</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="generator" content="TeX4ht (http://www.tug.org/tex4ht/)">
<meta name="originator" content="TeX4ht (http://www.tug.org/tex4ht/)">
<!-- 3,html -->
<meta name="src" content="nipguide.tex">
<link rel="stylesheet" type="text/css" href="nipguide.css">
</head><body
>
<!--l. 333--><div class="crosslinks"><p class="noindent">[<a
href="nipguidese29.html" >next</a>] [<a
href="nipguidese26.html" >prev</a>] [<a
href="nipguidese26.html#tailnipguidese26.html" >prev-tail</a>] [<a
href="#tailnipguidese27.html">tail</a>] [<a
href="nipguidech6.html#nipguidese28.html" >up</a>] </p></div>
<h3 class="sectionHead"><span class="titlemark">6.6 </span> <a
id="x38-630006.6"></a>Operators</h3>
<!--l. 334--><p class="noindent" ><a name="nip_label_sec:operators"></a>
<!--l. 336--><p class="indent" > <span
class="phvr7t-x-x-80">nip2</span>’s expression syntax is almost exactly the same as C,
with a few small changes. Table <a
href="#x38-630012">6.2<!--tex4ht:ref: tb:precedence --></a> lists all of <span
class="phvr7t-x-x-80">nip2</span>’s
operators in order of increasing precedence. If you’ve used
C, the differences are:
<ul class="itemize1">
<li class="itemize">C’s <span class="obeylines-h"><span class="verb"><span
class="pcrr7t-">?:</span></span></span> operator becomes <span
class="phvr7t-x-x-80">if-then-else</span>, see above
</li>
<li class="itemize">Like almost every functional language, <span
class="phvr7t-x-x-80">nip2 </span>uses
square brackets for list constants (see <span
class="cmsy-10"></span><a
href="#x38-680006.6.5">6.6.5<!--tex4ht:ref: sec:listsyntax --></a>), so to
index a list, <span
class="phvr7t-x-x-80">nip2 </span>uses <span
class="phvr7t-x-x-80">?</span>
</li>
<li class="itemize"><span
class="phvr7t-x-x-80">nip2 </span>adds <span
class="phvr7t-x-x-80">@ </span>for function composition, see <span
class="cmsy-10"></span><a
href="#x38-690006.6.6">6.6.6<!--tex4ht:ref: sec:func --></a>
</li>
<li class="itemize">The <span
class="phvr7t-x-x-80">: </span>operator is infix list cons, see <span
class="cmsy-10"></span><a
href="nipguidese28.html#x39-710006.7">6.7<!--tex4ht:ref: sec:lists --></a>
</li>
<li class="itemize">The <span
class="phvr7t-x-x-80">++ </span>operator becomes an infix concatenation
operator, <span
class="phvr7t-x-x-80">– </span>becomes list difference. Again, see
<span
class="cmsy-10"></span><a
href="#x38-680006.6.5">6.6.5<!--tex4ht:ref: sec:listsyntax --></a>
</li></ul>
<!--l. 363--><p class="indent" > The only slightly tricky point is that function application
binds very tightly (only list index and class project bind
more tightly). So the expression:
<div class="verbatim" id="verbatim-32">
jim = fred 2 + 3
</div>
<!--l. 369--><p class="nopar" >
<!--l. 371--><p class="noindent" >binds as:
<div class="verbatim" id="verbatim-33">
jim = (fred 2) + 3
</div>
<!--l. 376--><p class="nopar" >
<!--l. 378--><p class="noindent" >This is almost always the behaviour you want.
<!--l. 381--><p class="indent" > There are two special equality tests: <span
class="phvr7t-x-x-80">=== </span>and <span
class="phvr7t-x-x-80">!==</span>. These
test for pointer equality, that is, they return <span
class="phvr7t-x-x-80">true </span>if their
arguments refer to the same object. These are occasionally
useful for writing interactive functions.
<!--l. 387--><p class="indent" > <a
id="x38-630012"></a><hr class="float"><div class="float"
>
<div class="center"
>
<!--l. 387--><p class="noindent" >
<div class="tabular"> <table id="TBL-4" class="tabular"
cellspacing="0" cellpadding="0"
><colgroup id="TBL-4-1g"><col
id="TBL-4-1"></colgroup><colgroup id="TBL-4-2g"><col
id="TBL-4-2"></colgroup><colgroup id="TBL-4-3g"><col
id="TBL-4-3"></colgroup><tr
class="hline"><td><hr></td><td><hr></td><td><hr></td></tr><tr
style="vertical-align:baseline;" id="TBL-4-1-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-1-1"
class="td11">Operator</td><td style="white-space:nowrap; text-align:left;" id="TBL-4-1-2"
class="td11">Associativity</td><td style="white-space:nowrap; text-align:left;" id="TBL-4-1-3"
class="td11">Description </td>
</tr><tr
class="hline"><td><hr></td><td><hr></td><td><hr></td></tr><tr
style="vertical-align:baseline;" id="TBL-4-2-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-2-1"
class="td11"><span
class="phvr7t-x-x-80">if then else</span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-2-2"
class="td11">Right </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-2-3"
class="td11">If-then-else construct </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-3-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-3-1"
class="td11"><span
class="phvr7t-x-x-80">= </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-3-2"
class="td11">Left </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-3-3"
class="td11">Form name/value pair </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-4-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-4-1"
class="td11"><span class="obeylines-h"><span class="verb"><span
class="pcrr7t-">||</span></span></span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-4-2"
class="td11">Left </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-4-3"
class="td11">Logical or </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-5-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-5-1"
class="td11"><span
class="phvr7t-x-x-80">&& </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-5-2"
class="td11">Left </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-5-3"
class="td11">Logical and </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-6-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-6-1"
class="td11"><span
class="phvr7t-x-x-80">@ </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-6-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-6-3"
class="td11">Function composition (see <span
class="cmsy-10"></span><a
href="#x38-690006.6.6">6.6.6<!--tex4ht:ref: sec:func --></a>)</td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-7-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-7-1"
class="td11"><span class="obeylines-h"><span class="verb"><span
class="pcrr7t-">|</span></span></span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-7-2"
class="td11">Left </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-7-3"
class="td11">Bitwise or </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-8-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-8-1"
class="td11"><img
src="nipguide0x.png" alt="^ " class="circ" > </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-8-2"
class="td11">Left </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-8-3"
class="td11">Bitwise exclusive or </td></tr><tr
style="vertical-align:baseline;" id="TBL-4-9-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-9-1"
class="td11"><span
class="phvr7t-x-x-80">& </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-9-2"
class="td11">Left </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-9-3"
class="td11">Bitwise and</td>
</tr><tr
class="hline"><td><hr></td><td><hr></td><td><hr></td></tr><tr
style="vertical-align:baseline;" id="TBL-4-10-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-10-1"
class="td11"><span
class="phvr7t-x-x-80">== </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-10-2"
class="td11">Left </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-10-3"
class="td11">Equal to </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-11-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-11-1"
class="td11"><span
class="phvr7t-x-x-80">!= </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-11-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-11-3"
class="td11">Not equal to </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-12-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-12-1"
class="td11"><span
class="phvr7t-x-x-80">=== </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-12-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-12-3"
class="td11">Pointer equal to </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-13-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-13-1"
class="td11"><span
class="phvr7t-x-x-80">!== </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-13-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-13-3"
class="td11">Pointer not equal to </td>
</tr><tr
class="hline"><td><hr></td><td><hr></td><td><hr></td></tr><tr
style="vertical-align:baseline;" id="TBL-4-14-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-14-1"
class="td11"><span
class="phvr7t-x-x-80"> </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-14-2"
class="td11">Left </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-14-3"
class="td11">Less than </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-15-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-15-1"
class="td11"><span
class="phvr7t-x-x-80">= </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-15-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-15-3"
class="td11">Less than or equal to </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-16-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-16-1"
class="td11"><span
class="phvr7t-x-x-80"> </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-16-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-16-3"
class="td11">Greater than </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-17-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-17-1"
class="td11"><span
class="phvr7t-x-x-80">= </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-17-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-17-3"
class="td11">Greater than or equal to </td>
</tr><tr
class="hline"><td><hr></td><td><hr></td><td><hr></td></tr><tr
style="vertical-align:baseline;" id="TBL-4-18-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-18-1"
class="td11"><span
class="phvr7t-x-x-80"> </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-18-2"
class="td11">Left </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-18-3"
class="td11">Left shift </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-19-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-19-1"
class="td11"><span
class="phvr7t-x-x-80"> </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-19-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-19-3"
class="td11">Right shift </td>
</tr><tr
class="hline"><td><hr></td><td><hr></td><td><hr></td></tr><tr
style="vertical-align:baseline;" id="TBL-4-20-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-20-1"
class="td11"><span
class="phvr7t-x-x-80">+ </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-20-2"
class="td11">Left </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-20-3"
class="td11">Addition </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-21-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-21-1"
class="td11"><span
class="phvr7t-x-x-80">- </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-21-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-21-3"
class="td11">Subtraction </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-22-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-22-1"
class="td11"><span
class="phvr7t-x-x-80">* </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-22-2"
class="td11">Left </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-22-3"
class="td11">Multiplication </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-23-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-23-1"
class="td11"><span
class="cmmi-10">∕ </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-23-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-23-3"
class="td11">Division </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-24-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-24-1"
class="td11"><span
class="phvr7t-x-x-80">% </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-24-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-24-3"
class="td11">Remainder after division </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-25-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-25-1"
class="td11"><span
class="phvr7t-x-x-80">! </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-25-2"
class="td11">Left </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-25-3"
class="td11">Logical negation </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-26-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-26-1"
class="td11"><span class="obeylines-h"><span class="verb"><span
class="pcrr7t-">~</span></span></span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-26-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-26-3"
class="td11">One’s complement </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-27-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-27-1"
class="td11"><span
class="phvr7t-x-x-80">++ </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-27-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-27-3"
class="td11">Join (see <span
class="cmsy-10"></span><a
href="#x38-680006.6.5">6.6.5<!--tex4ht:ref: sec:listsyntax --></a>) </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-28-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-28-1"
class="td11"><span class="obeylines-h"><span class="verb"><span
class="pcrr7t-">--</span></span></span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-28-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-28-3"
class="td11">Difference (see <span
class="cmsy-10"></span><a
href="#x38-680006.6.5">6.6.5<!--tex4ht:ref: sec:listsyntax --></a>) </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-29-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-29-1"
class="td11"><span
class="phvr7t-x-x-80">- </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-29-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-29-3"
class="td11">Unary minus </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-30-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-30-1"
class="td11"><span
class="phvr7t-x-x-80">+ </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-30-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-30-3"
class="td11">Unary plus </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-31-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-31-1"
class="td11"><span
class="phvr7t-x-x-80">(</span><span
class="ptmri7t-">type</span><span
class="phvr7t-x-x-80">) </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-31-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-31-3"
class="td11">Type cast expression </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-32-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-32-1"
class="td11"><span
class="phvr7t-x-x-80">** </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-32-2"
class="td11">Right </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-32-3"
class="td11">Raise to power </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-33-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-33-1"
class="td11"><span
class="phvr7t-x-x-80">: </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-33-2"
class="td11"> </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-33-3"
class="td11">List CONS (see <span
class="cmsy-10"></span><a
href="#x38-680006.6.5">6.6.5<!--tex4ht:ref: sec:listsyntax --></a>) </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-34-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-34-1"
class="td11"><span
class="ptmri7t-">space </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-34-2"
class="td11">Left </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-34-3"
class="td11">Function application </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-35-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-35-1"
class="td11"><span
class="phvr7t-x-x-80">? </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-35-2"
class="td11">Left </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-35-3"
class="td11">List index (see <span
class="cmsy-10"></span><a
href="#x38-680006.6.5">6.6.5<!--tex4ht:ref: sec:listsyntax --></a>) </td>
</tr><tr
style="vertical-align:baseline;" id="TBL-4-36-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-36-1"
class="td11"><span
class="phvr7t-x-x-80">. </span></td><td style="white-space:nowrap; text-align:left;" id="TBL-4-36-2"
class="td11">Left </td><td style="white-space:nowrap; text-align:left;" id="TBL-4-36-3"
class="td11">Class project (see <span
class="cmsy-10"></span><a
href="nipguidese32.html#x43-750006.11">6.11<!--tex4ht:ref: sec:class --></a>) </td>
</tr><tr
class="hline"><td><hr></td><td><hr></td><td><hr></td></tr><tr
style="vertical-align:baseline;" id="TBL-4-37-"><td style="white-space:nowrap; text-align:left;" id="TBL-4-37-1"
class="td11"> </td></tr></table></div></div>
<br /><div class="caption"
><span class="id">Table 6.2: </span><span
class="content"><span
class="phvr7t-x-x-80">nip2 </span>operators in order of increasing precedence</span></div><!--tex4ht:label?: x38-630012 -->
<!--l. 441--><p class="noindent" ><a name="nip_label_tb:precedence"></a>
</div><hr class="endfloat" />
<h4 class="subsectionHead"><span class="titlemark">6.6.1 </span> <a
id="x38-640006.6.1"></a>The real type</h4>
<!--l. 446--><p class="noindent" ><span
class="phvr7t-x-x-80">nip2 </span>has a single number type for integers and real numbers.
All are represented internally as 64-bit floating point values.
You can use the four standard arithmetic operators (<span
class="phvr7t-x-x-80">+</span>, <span
class="phvr7t-x-x-80">-</span>, <span
class="phvr7t-x-x-80">*</span>, <span
class="phvr7t-x-x-80">/</span>),
remainder after integer division (%), raise-to-power (<span
class="phvr7t-x-x-80">**</span>), the
relational operators (<span
class="phvr7t-x-x-80"></span>, <span
class="phvr7t-x-x-80">=</span>, <span
class="phvr7t-x-x-80"></span>, <span
class="phvr7t-x-x-80">=</span>, <span
class="phvr7t-x-x-80">==</span>), the bitwise logical
operators (<span
class="phvr7t-x-x-80">&</span>, <span class="obeylines-h"><span class="verb"><span
class="pcrr7t-">|</span></span></span>, <img
src="nipguide1x.png" alt="^ " class="circ" >, <span class="obeylines-h"><span class="verb"><span
class="pcrr7t-">~</span></span></span>), integer shift operators (<span
class="phvr7t-x-x-80"></span>, <span
class="phvr7t-x-x-80"></span>) and
unary negation and positive (<span
class="phvr7t-x-x-80">-</span>, <span
class="phvr7t-x-x-80">+</span>).
<!--l. 454--><p class="indent" > Other mathematical functions are pre-defined for you: <span
class="phvr7t-x-x-80">sin</span>,
<span
class="phvr7t-x-x-80">cos</span>, <span
class="phvr7t-x-x-80">tan</span>, <span
class="phvr7t-x-x-80">asin</span>, <span
class="phvr7t-x-x-80">acos</span>, <span
class="phvr7t-x-x-80">atan</span>, <span
class="phvr7t-x-x-80">log</span>, <span
class="phvr7t-x-x-80">log10</span>, <span
class="phvr7t-x-x-80">exp</span>, <span
class="phvr7t-x-x-80">exp10</span>, <span
class="phvr7t-x-x-80">ceil</span>, <span
class="phvr7t-x-x-80">floor</span>. Each
has the standard behaviour.
<!--l. 458--><p class="indent" > You can use type-casts on reals. However, they remain
64-bit floating point, the range is simply clipped. Casting to
<span
class="phvr7t-x-x-80">unsigned short </span>produces a 64-bit float whose fractional part
has been set to zero, and which has been clipped to the
range 0 to 65535. This may or may not cause rounding
problems.
<!--l. 463--><p class="indent" > You can write hexadecimal number constants as
<span class="obeylines-h"><span class="verb"><span
class="pcrr7t-">0xff</span></span></span>.
<!--l. 465--><p class="noindent" >
<h4 class="subsectionHead"><span class="titlemark">6.6.2 </span> <a
id="x38-650006.6.2"></a>The complex type</h4>
<!--l. 467--><p class="noindent" >Complex numbers are rather sketchily implemented. They
are generally handy for representing vectors and coordinates
rather than for doing arithmetic, so the range of operations
is limited.
<!--l. 471--><p class="indent" > Complex constants are written as two numbers enclosed
in round brackets and separated by a comma. You can
use the four standard arithmetic operators (<span
class="phvr7t-x-x-80">+</span>, <span
class="phvr7t-x-x-80">-</span>, <span
class="phvr7t-x-x-80">*</span>, <span
class="phvr7t-x-x-80">/</span>),
raise-to-power (<span
class="phvr7t-x-x-80">**</span>), and unary negation and positive (<span
class="phvr7t-x-x-80">-</span>, <span
class="phvr7t-x-x-80">+</span>).
You can use <span
class="phvr7t-x-x-80">== </span>only of the relational operators. You can mix
complex and real numbers in expressions. You can cast reals
to complex and back. Use the functions <span
class="phvr7t-x-x-80">re </span>and <span
class="phvr7t-x-x-80">im </span>to extract
the real and imaginary parts.
<div class="verbatim" id="verbatim-34">
(12, 13) + 4 == (16, 13)
 <br />(12, 2 + 2) ==  (12, 4)
 <br />re (12, 13) == 12
 <br />im (12, 13) == 13
</div>
<!--l. 484--><p class="nopar" >
<!--l. 486--><p class="noindent" >
<h4 class="subsectionHead"><span class="titlemark">6.6.3 </span> <a
id="x38-660006.6.3"></a>The character type</h4>
<!--l. 488--><p class="noindent" >Character constants are written as single characters enclosed
in single quotes. You can use the relational operators (<span
class="phvr7t-x-x-80"></span>, <span
class="phvr7t-x-x-80">=</span>,
<span
class="phvr7t-x-x-80"></span>, <span
class="phvr7t-x-x-80">=</span>, <span
class="phvr7t-x-x-80">==</span>) to sort characters by ASCII order. You can
cast a character to a real to get its ASCII value. You
can cast a real ASCII value to a character. You can
use the standard C escapes to represent non-ASCII
characters.
<div class="verbatim" id="verbatim-35">
(int) 'A' == 65
 <br />(char) 65 == 'A'
 <br />is_digit x = '0' <= x && x <= '9'
 <br />newline == '\n'
</div>
<!--l. 500--><p class="nopar" >
<!--l. 502--><p class="noindent" >
<h4 class="subsectionHead"><span class="titlemark">6.6.4 </span> <a
id="x38-670006.6.4"></a>The boolean type</h4>
<!--l. 504--><p class="noindent" >The two boolean constants are written as <span
class="phvr7t-x-x-80">true </span>and <span
class="phvr7t-x-x-80">false</span>.
Boolean values are generated by the relational operators.
You can use the standard logical operators (<span
class="phvr7t-x-x-80">&&</span>, <span class="obeylines-h"><span class="verb"><span
class="pcrr7t-">||</span></span></span>, <span
class="phvr7t-x-x-80">!</span>). You
can use a boolean type as an argument in an <span
class="phvr7t-x-x-80">if-then-else</span>
expression.
<!--l. 509--><p class="indent" > As with C, the logical operators do not evaluate their
right-hand sides if their value can be determined just from
evaluating their left-hand sides.
<div class="verbatim" id="verbatim-36">
true && false == false
 <br />true || error "boink!" == true
 <br />if true then 12 else 13 == 12
</div>
<!--l. 516--><p class="nopar" >
<!--l. 518--><p class="noindent" >
<h4 class="subsectionHead"><span class="titlemark">6.6.5 </span> <a
id="x38-680006.6.5"></a>The list type</h4>
<!--l. 519--><p class="noindent" ><a name="nip_label_sec:listsyntax"></a>
<!--l. 521--><p class="indent" > Lists are created from two constructors. <span
class="phvr7t-x-x-80">[] </span>denotes the
empty list. The list construction operator (<span
class="phvr7t-x-x-80">:</span>, pronounced
CONS by LISP programmers) takes an item and a list, and
returns a new list with the item added to the front. As a
convenience, <span
class="phvr7t-x-x-80">nip2 </span>has a syntax for list constants. A list
constant is a list of items, separated by commas, and
enclosed in square brackets:
<div class="verbatim" id="verbatim-37">
12:[] == [12]
 <br />12:13:14:[] == 12:(13:(14:[])) ==
 <br />  [12,13,14]
 <br />[a+2,3,4] == (a+2):3:4:[]
 <br />[2]:[3,4] == [[2],3,4]
</div>
<!--l. 533--><p class="nopar" >
<!--l. 535--><p class="indent" > Use the functions <span
class="phvr7t-x-x-80">hd </span>and <span
class="phvr7t-x-x-80">tl </span>to take the head and the tail of
a list:
<div class="verbatim" id="verbatim-38">
hd [12,13,14] == 12
 <br />tl [12,13,14] == [13,14]
</div>
<!--l. 541--><p class="nopar" >
<!--l. 543--><p class="indent" > Use <span
class="phvr7t-x-x-80">.. </span>in a list constant to define a list generator. List
generators build lists of numbers for you:
<div class="verbatim" id="verbatim-39">
[1..10] == [1,2,3,4,5,6,7,8,9,10]
 <br />[1,3..10] == [1,3,5,7,9]
 <br />[10,9..1] == [10,9,8,7,6,5,4,3,2,1]
</div>
<!--l. 550--><p class="nopar" >
<!--l. 552--><p class="noindent" >List generators are useful for expressing iteration.
<!--l. 555--><p class="indent" > Lists may be infinite:
<div class="verbatim" id="verbatim-40">
[1..] == [1,2,3,4,5,6,7,8,9 ..]
 <br />[5,4..] == [5,4,3,2,1,0,-1,-2,-3 ..]
</div>
<!--l. 560--><p class="nopar" >
<!--l. 562--><p class="noindent" >Infinite lists are useful for expressing unbounded iteration.
See <span
class="cmsy-10"></span><a
href="nipguidese29.html#x40-720006.8">6.8<!--tex4ht:ref: sec:lazy --></a>.
<!--l. 566--><p class="indent" > You can write list comprehensions like this:
<div class="verbatim" id="verbatim-41">
[x :: x <- [1..]; x % 2 == 0]
</div>
<!--l. 570--><p class="nopar" >
<!--l. 572--><p class="noindent" >This could be read as <span
class="ptmri7t-">All x such that x is in </span><span class="obeylines-h"><span class="verb"><span
class="pcrr7t-">[1..]</span></span></span> <span
class="ptmri7t-">and x is</span>
<span
class="ptmri7t-">even</span>, that is, the list of even numbers.
<!--l. 576--><p class="indent" > You can have any number of semicolon-separated
qualifiers and each one can be either a generator (like
<span class="obeylines-h"><span class="verb"><span
class="pcrr7t-">x</span><span
class="pcrr7t-"> <-</span><span
class="pcrr7t-"> [1..]</span></span></span>) introducing a new variable or pattern (see
<span
class="cmsy-10"></span><a
href="nipguidese30.html#x41-730006.9">6.9<!--tex4ht:ref: sec:pattern --></a>), or a predicate (like <span class="obeylines-h"><span class="verb"><span
class="pcrr7t-">x</span><span
class="pcrr7t-"> %</span><span
class="pcrr7t-"> 2</span><span
class="pcrr7t-"> ==</span><span
class="pcrr7t-"> 0</span></span></span>) which filters the
generators to the left of it.
<!--l. 581--><p class="indent" > Later generators change more rapidly, so for example:
<div class="verbatim" id="verbatim-42">
[(x, y) ::
 <br />  x <- [1..3]; y <- [x..3]] ==
 <br />    [(1, 1), (1, 2), (1, 3),
 <br />      (2, 2), (2, 3), (3, 3)]
</div>
<!--l. 588--><p class="nopar" >
<!--l. 590--><p class="indent" > You can nest list comprehensions to generate more
complex data structures. For example:
<div class="verbatim" id="verbatim-43">
[[x ⋆ y :: x <- [1..10]] ::
 <br />  y <- [1..10]]
</div>
<!--l. 596--><p class="nopar" >
<!--l. 598--><p class="noindent" >will generate a times-table.
<!--l. 601--><p class="indent" > You can use pattern-matching (see <span
class="cmsy-10"></span><a
href="nipguidese30.html#x41-730006.9">6.9<!--tex4ht:ref: sec:pattern --></a>) to loop over
several generators at the same time. For example:
<div class="verbatim" id="verbatim-44">
[(x, y) :: [x, y] <-
 <br />  zip2 [1..3] [1..3]] ==
 <br />    [(1, 1), (2, 2), (3, 3)]
</div>
<!--l. 608--><p class="nopar" >
<!--l. 610--><p class="indent" > As a convenience, lists of characters may be written
enclosed in double quotes:
<div class="verbatim" id="verbatim-45">
"abc" == ['a','b','c']
</div>
<!--l. 615--><p class="nopar" >
<!--l. 617--><p class="indent" > You can define a string constant which has the same form
as a variable name (that is, letters, numbers, underscore and
apostrophy only) with a <span class="obeylines-h"><span class="verb"><span
class="pcrr7t-">$</span></span></span> prefix. For example:
<div class="verbatim" id="verbatim-46">
$form7 == "form7"
</div>
<!--l. 623--><p class="nopar" >
<!--l. 625--><p class="noindent" ><span
class="phvr7t-x-x-80">nip2 </span>often uses these in option lists.
<!--l. 628--><p class="indent" > You can define a name, value pair with the <span
class="phvr7t-x-x-80">= </span>operator.
<div class="verbatim" id="verbatim-47">
$fred => 12 == ["fred", 12]
</div>
<!--l. 632--><p class="nopar" >
<!--l. 634--><p class="noindent" >Again, these pairs are frequently used to pass options to
objects.
<!--l. 637--><p class="indent" > A list may contain any object:
<div class="verbatim" id="verbatim-48">
[1,'a',true,[1,2,3]]
</div>
<!--l. 641--><p class="nopar" >
<!--l. 643--><p class="noindent" >Mixing types in a list tends to be confusing and should be
avoided. If you want to group a set of diverse objects, define
a class instead, see <span
class="cmsy-10"></span><a
href="nipguidese32.html#x43-750006.11">6.11<!--tex4ht:ref: sec:class --></a>.
<!--l. 648--><p class="indent" > Lists of lists of reals are useful for representing arrays.
<!--l. 650--><p class="indent" > You can use the list index operator (<span
class="phvr7t-x-x-80">?</span>) to extract an
element from a position in a list:
<div class="verbatim" id="verbatim-49">
[1,2,3] ? 0 == 1
 <br />"abc" ? 1 == 'b'
</div>
<!--l. 656--><p class="nopar" >
<!--l. 658--><p class="indent" > You can use the list join operator (<span
class="phvr7t-x-x-80">++</span>) to join two lists
together end-to-end.
<div class="verbatim" id="verbatim-50">
[1,2,3] ++ [4,5,6] == [1,2,3,4,5,6]
</div>
<!--l. 663--><p class="nopar" >
<!--l. 665--><p class="indent" > You can use the list difference operator (<span class="obeylines-h"><span class="verb"><span
class="pcrr7t-">--</span></span></span>) to remove
elements of one list from another.
<div class="verbatim" id="verbatim-51">
[1..10] -- [4,5,6] == [1,2,3,7,8,9,10]
</div>
<!--l. 670--><p class="nopar" >
<!--l. 672--><p class="noindent" >
<h4 class="subsectionHead"><span class="titlemark">6.6.6 </span> <a
id="x38-690006.6.6"></a>The function type</h4>
<!--l. 673--><p class="noindent" ><a name="nip_label_sec:func"></a>
<!--l. 675--><p class="indent" > Functions are objects just like any other. You can pass
functions to other functions as parameters, store functions in
lists, and so on.
<!--l. 678--><p class="indent" > You can create anonymous functions with <span class="obeylines-h"><span class="verb"><span
class="pcrr7t-">\</span></span></span> (lambda).
For example:
<div class="verbatim" id="verbatim-52">
map (\x x + 2) [1..3] == [3, 4, 5]
</div>
<!--l. 682--><p class="nopar" >
<!--l. 684--><p class="indent" > You can nest lambdas to make multi-argument anonymous
functions, for example:
<div class="verbatim" id="verbatim-53">
map2 (\x\y x + y) [1..3] [2..5] ==
 <br />  [3, 5, 7]
</div>
<!--l. 689--><p class="nopar" >
<!--l. 691--><p class="indent" > You can compose functions with the <span
class="phvr7t-x-x-80">@ </span>operator.
For example, for two functions of one argument <span
class="phvr7t-x-x-80">f </span>and
<span
class="phvr7t-x-x-80">g</span>:
<div class="verbatim" id="verbatim-54">
f (g 2) == (f @ g) 2
</div>
<!--l. 696--><p class="nopar" >
<!--l. 698--><p class="noindent" >
<h4 class="subsectionHead"><span class="titlemark">6.6.7 </span> <a
id="x38-700006.6.7"></a>The image type</h4>
<!--l. 700--><p class="noindent" >These represent a low-level handle to a VIPS image
structure. You can make them with the <span
class="phvr7t-x-x-80">vips</span><span
class="phvr7t-x-x-80">_image </span>builtin,
and you can pass them as parameters to VIPS functions.
The <span
class="phvr7t-x-x-80">Image </span>class is built on top of them, see <span
class="cmsy-10"></span><a
href="nipguidese33.html#x44-840006.12.3">6.12.3<!--tex4ht:ref: sec:Image --></a>.
<!--l. 705--><p class="indent" > As an accident of history, <span
class="phvr7t-x-x-80">nip2 </span>also lets you do arithmetic
with them. This will probably be removed in the next
version or two, so it’s best to go through the higher-level
<span
class="phvr7t-x-x-80">Image </span>class.
<!--l. 709--><div class="crosslinks"><p class="noindent">[<a
href="nipguidese29.html" >next</a>] [<a
href="nipguidese26.html" >prev</a>] [<a
href="nipguidese26.html#tailnipguidese26.html" >prev-tail</a>] [<a
href="nipguidese27.html" >front</a>] [<a
href="nipguidech6.html#nipguidese28.html" >up</a>] </p></div>
<!--l. 709--><p class="indent" > <a
id="tailnipguidese27.html"></a>
</body></html>
|