[Commit] nickle-www/tutorial c1075.html,NONE,1.1 c131.html,NONE,1.1 c32.html,NONE,1.1 c840.html,NONE,1.1 x1012.html,NONE,1.1 x1133.html,NONE,1.1 x1218.html,NONE,1.1 x1308.html,NONE,1.1 x1436.html,NONE,1.1 x368.html,NONE,1.1 x655.html,NONE,1.1 x78.html,NONE,1.1 x818.html,NONE,1.1 x967.html,NONE,1.1 c1147.html,1.1,NONE c13.html,1.1,NONE c133.html,1.1,NONE c34.html,1.1,NONE c863.html,1.1,NONE index.html,1.16,NONE x1015.html,1.1,NONE x1074.html,1.1,NONE x1205.html,1.1,NONE x1295.html,1.1,NONE x1392.html,1.1,NONE x1544.html,1.1,NONE x370.html,1.1,NONE x657.html,1.1,NONE x80.html,1.1,NONE x841.html,1.1,NONE

commit at keithp.com commit at keithp.com
Fri Jun 6 13:34:43 PDT 2003


Committed by: p186

Update of /local/src/CVS/nickle-www/tutorial
In directory home.keithp.com:/tmp/cvs-serv6934

Added Files:
	c1075.html c131.html c32.html c840.html x1012.html x1133.html 
	x1218.html x1308.html x1436.html x368.html x655.html x78.html 
	x818.html x967.html 
Removed Files:
	c1147.html c13.html c133.html c34.html c863.html index.html 
	x1015.html x1074.html x1205.html x1295.html x1392.html 
	x1544.html x370.html x657.html x80.html x841.html 
Log Message:
Changed tutorial DocBook source.


--- NEW FILE: c1075.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Advanced topics</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Nickle Tutorial"
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="Strings"
HREF="x1012.html"><LINK
REL="NEXT"
TITLE="Nickle Namespaces"
HREF="x1133.html"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Nickle Tutorial</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x1012.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x1133.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="AEN1075"
></A
>Chapter 5. Advanced topics</H1
><BLOCKQUOTE
CLASS="ABSTRACT"
><DIV
CLASS="ABSTRACT"
><P
></P
><A
NAME="AEN1077"
></A
><P
>This chapter will discuss more advanced topics; these features make Nickle as powerful as it is.
The semantics of copying and garbage collection, namespaces, exceptions, threading and mutual exclusion, and continuations will all be covered.</P
><P
></P
></DIV
></BLOCKQUOTE
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN1079"
>5.1. Copy Semantics and Garbage Collection</A
></H1
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN1081"
>5.1.1. Copy by value</A
></H2
><P
>In Nickle, assignment, argument passing, and definitions--in short everything involving the values of variables--are all by-value.
Nickle avoids the weird C-isms, like being by-value except for arrays and strings.
Everything is copied.
Consider the following example:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1084"
></A
><PRE
CLASS="SCREEN"
>&#62; int[*] foo = { 1, 2, 3 };
&#62; int[*] bar = foo;
&#62; foo[2] = 4;
&#62; foo
[3] {1, 2, 4}</PRE
><P
></P
></DIV
></P
><P
>What will <VAR
CLASS="LITERAL"
>bar[2]</VAR
> be?
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1088"
></A
><PRE
CLASS="SCREEN"
>&#62; bar
[3] {1, 2, 3}</PRE
><P
></P
></DIV
></P
><P
>Since assignment is by-value, bar has its own values--it is unchanged.
Also consider function arguments:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1091"
></A
><PRE
CLASS="SCREEN"
>&#62; string s = "hello, world"; 
&#62; (void func(string s) { s = "foobar"; printf("%s\n",s); })(s);
foobar</PRE
><P
></P
></DIV
></P
><P
>Does <VAR
CLASS="LITERAL"
>s</VAR
> still have its original value, or "foobar"?
Since the function was modifying a copy--which was passed by-value--<VAR
CLASS="LITERAL"
>s</VAR
> will be unchanged.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1096"
></A
><PRE
CLASS="SCREEN"
>&#62; s
"hello, world"</PRE
><P
></P
></DIV
></P
><P
>What if you want to pass something by reference?
Nickle has a reference type to accomplish just that.
(You could also use pointers, but references are The Right Way.
Anyway, pointers may eventually be removed from the language in preference to references.)
For example, to reimplement the example above using references:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1099"
></A
><PRE
CLASS="SCREEN"
>&#62; string s = "hello, world";
&#62; (void func(&#38;string s) { s = "foobar"; printf("%s\n",s); })(&#38;s);
foobar
&#62; s
"foobar"</PRE
><P
></P
></DIV
></P
><P
>Notice that <VAR
CLASS="LITERAL"
>s</VAR
> was changed; it was passed as a reference (&amp;string).
See the section on Variables for a discussion of references.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN1103"
>5.1.2. Garbage collection</A
></H2
><P
>But if all those strings and arrays are copied entirely every time a function is called or an assignment made, won't there be a lot of unused, unreferenceable memory lying around?
No. Nickle is fully garbage-collected; when a value no longer has any names, it is freed.
This is invisible to the user/programmer, who need not worry about allocation, deallocation, or any other aspects of their assignments and argument passing.</P
><P
>In short, everything is by-value, and Nickle takes care of allocation and deallocation.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN1107"
>5.1.3. Type checking and subtyping</A
></H2
><P
>Type checking in Nickle is a combination of compile-time and runtime checking.
At compile-time, Nickle will ensure that all assignments, argument passing, and other copying situations are sane, for instance that no strings are being assigned to integers.
It will let some errors through if it cannot be sure they are errors.
For instance, variables of type 'poly' can hold any type; at compile-time, nothing is ruled out, but this does not mean you can't break typing at run-time.</P
><P
>At runtime, Nickle makes sure all assignments are actually valid.
It does so by determining if one type is a subtype of the other, i.e. if the set of all values that can fit in it also fit into the other.
As a concrete example:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1111"
></A
><PRE
CLASS="SCREEN"
>&#62; int i = 1;
&#62; rational r = i;
&#62; i = r/3;
Unhandled exception "invalid_argument" at &#60;stdin&#62;:8
        (1/3)
        0
        "Incompatible types in assignment"</PRE
><P
></P
></DIV
></P
><P
>The int can hold the integer value 1 without difficulty, because they are the same type.
The rational can accept the same value because integers are a subset of rationals.
However, attempting to assign a rational (1/3) to the integer raises an exception.
This demonstrates that int is a subtype of rational; conversely, rational is the supertype of int.
A variable can take on a value from any of its subtypes, but not from its supertypes--and if the two values do not share a sub/supertype relationship, they will not get pass the compile-time
checker.</P
><P
>A similar check occurs with structs.
If one struct's elements are a subset of another's, it may take that value.
For example,
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1115"
></A
><PRE
CLASS="SCREEN"
>&#62; typedef struct { int i; string s; } i_and_s;
&#62; typedef struct { int i; } just_i;
&#62; i_and_s is = { i=2, s="hello" };
&#62; just_i i = is;   
&#62; just_i ji = { i=2 };
&#62; is = ji;
Unhandled exception "invalid_argument" at &#60;stdin&#62;:17
        {i = 2}
        0
        "Incompatible types in assignment"</PRE
><P
></P
></DIV
></P
><P
>Since <VAR
CLASS="LITERAL"
>just_i</VAR
> is a subtype of <VAR
CLASS="LITERAL"
>i_and_s</VAR
> (it has <VAR
CLASS="LITERAL"
>i</VAR
> but not <VAR
CLASS="LITERAL"
>s</VAR
>), the assignment to <VAR
CLASS="LITERAL"
>i</VAR
> from <VAR
CLASS="LITERAL"
>is</VAR
> worked.
However, attempting to assign to <VAR
CLASS="LITERAL"
>is</VAR
> from a <VAR
CLASS="LITERAL"
>just_i</VAR
> failed, because it did not have an <VAR
CLASS="LITERAL"
>s</VAR
> to copy over.</P
><P
>Finally, in assignments of one function to another, the following must be the case:</P
><P
></P
><UL
><LI
><P
>The arguments of the right-side function must be able to be assigned to those of the left-side function.
In other words, that on the left must accept a subset of the arguments of that on the right.</P
></LI
><LI
><P
>The return type of the left-side function must be able to be assigned to that of the right-side function.
In other words, its value should be usable anywhere that of the one on the right could be used.</P
></LI
></UL
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x1012.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x1133.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Strings</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Nickle Namespaces</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
--- NEW FILE: c131.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Language introduction</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Nickle Tutorial"
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="Commands"
HREF="x78.html"><LINK
REL="NEXT"
TITLE="Nickle Expressions"
HREF="x368.html"></HEAD
><BODY
CLASS="CHAPTER"
[...1259 lines suppressed...]
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Commands</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Nickle Expressions</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
--- NEW FILE: c32.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Nickle Basics</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Nickle Tutorial"
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="Nickle Tour"
HREF="c13.html"><LINK
REL="NEXT"
TITLE="Commands"
HREF="x78.html"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Nickle Tutorial</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="c13.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x78.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="AEN32"
></A
>Chapter 2. Nickle Basics</H1
><BLOCKQUOTE
CLASS="ABSTRACT"
><DIV
CLASS="ABSTRACT"
><P
></P
><A
NAME="AEN34"
></A
><P
>Nickle is a powerful desktop calculator language with many features of advanced languages and support for arbitrary precision numbers.
It can run interactively to fulfill its role as a calculator, evaluate single expressions, and execute Nickle scripts.
It also has an array of useful top-level commands for interacting with the interpreter.</P
><P
></P
></DIV
></BLOCKQUOTE
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN36"
>2.1. Invocation</A
></H1
><P
><B
CLASS="COMMAND"
>nickle</B
>  [-f <VAR
CLASS="REPLACEABLE"
>file</VAR
>] [-l <VAR
CLASS="REPLACEABLE"
>file</VAR
>] [-e <VAR
CLASS="REPLACEABLE"
>expr</VAR
>] [<VAR
CLASS="REPLACEABLE"
>script</VAR
>]</P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><VAR
CLASS="LITERAL"
>-f</VAR
></DT
><DD
><P
>Evaluate <VAR
CLASS="REPLACEABLE"
>file</VAR
> first.
In addition, Nickle usually loads a <VAR
CLASS="LITERAL"
>.nicklerc</VAR
> file.</P
></DD
><DT
><VAR
CLASS="LITERAL"
>-l</VAR
></DT
><DD
><P
>Evaluate <VAR
CLASS="REPLACEABLE"
>file</VAR
> like <VAR
CLASS="LITERAL"
>-f</VAR
>, but expect it to be in <VAR
CLASS="LITERAL"
>$NICKLEPATH</VAR
>.</P
></DD
><DT
><VAR
CLASS="LITERAL"
>-e</VAR
></DT
><DD
><P
>Stop processing arguments and evaluate the rest of the line as a Nickle expression, print its value, and quit, e.g.</P
><DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN69"
></A
><PRE
CLASS="SCREEN"
>$ nickle -e 3**4
81
$</PRE
><P
></P
></DIV
></DD
><DT
><VAR
CLASS="LITERAL"
>script</VAR
></DT
><DD
><P
>If Nickle encounters an unflagged argument, it assumes it to be the name of a script, which it runs.
No more arguments are processed; the rest of the line is given to the script as its arguments.</P
></DD
></DL
></DIV
><P
>Without <VAR
CLASS="LITERAL"
>-e</VAR
> or a script as arguments, Nickle runs interactively, accepting standard input and writing to standard output.</P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="c13.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x78.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Nickle Tour</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Commands</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
--- NEW FILE: c840.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Builtins</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Nickle Tutorial"
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="Nickle Functions"
HREF="x818.html"><LINK
REL="NEXT"
TITLE="Math"
HREF="x967.html"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Nickle Tutorial</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x818.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x967.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="AEN840"
></A
>Chapter 4. Builtins</H1
><BLOCKQUOTE
CLASS="ABSTRACT"
><DIV
CLASS="ABSTRACT"
><P
></P
><A
NAME="AEN842"
></A
><P
>This chapter will explain various important builtin functions of Nickle, such as those for input and output and math.
It will also discuss the various operators and builtin functions that manipulate strings.</P
><P
></P
></DIV
></BLOCKQUOTE
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN844"
>4.1. Input and Output</A
></H1
><P
>Input and output in Nickle are mostly accomplished through the File builtin namespace; some top-level builtins refer to those functions.
Nickle's input and output are modeled, as much of the language is, on C, but many changes have been made.</P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN847"
>4.1.1. Opening and closing files</A
></H2
><P
>The functions in the File namespace use the <VAR
CLASS="LITERAL"
>file</VAR
> primitive type to describe filehandles.
Three are predefined, with their usual meanings: <VAR
CLASS="LITERAL"
>stdin</VAR
>, <VAR
CLASS="LITERAL"
>stdout</VAR
>, and <VAR
CLASS="LITERAL"
>stderr</VAR
>.
For many functions in File, there is a top-level builtin which assumes one of these standard streams.
Other files may be read and written by opening them:</P
><PRE
CLASS="SYNOPSIS"
>file open(string path, string mode)</PRE
><P
>The first string gives the path to the file to be opened; the second is one of:</P
><P
></P
><UL
><LI
><P
>"r" to open read-only, starting at the beginning of the file.</P
></LI
><LI
><P
>"r+" to open read-write, starting at the beginning of the file.</P
></LI
><LI
><P
>"w" to create or truncate the file and open write-only.</P
></LI
><LI
><P
>"w+" to create or truncate the file and open read-write.</P
></LI
><LI
><P
>"a" to open write-only, appending to the end of the file.</P
></LI
><LI
><P
>"a+" to open read-write, appending to the end of the file.</P
></LI
></UL
><P
>If successful, a filehandle will be returned that can then be used.</P
><P
>Nickle can also open pipes to other programs, reading or writing to their stdouts or stdins; these are also treated as <VAR
CLASS="LITERAL"
>file</VAR
>s, and the difference is transparent to the functions that manipulate them.
Pipes are opened with <VAR
CLASS="LITERAL"
>pipe</VAR
> rather than <VAR
CLASS="LITERAL"
>open</VAR
>; otherwise they are treated identically to flat files.</P
><PRE
CLASS="SYNOPSIS"
>file pipe(string path, string[*] argv, string mode)</PRE
><P
>The first string refers to the program to be run; <VAR
CLASS="LITERAL"
>argv</VAR
> is an array of the arguments to pass to it.
By convention, <VAR
CLASS="LITERAL"
>argv[0]</VAR
> should be the name of the program.
Finally, <VAR
CLASS="LITERAL"
>mode</VAR
> is one of those for <VAR
CLASS="LITERAL"
>open</VAR
>; reading from the pipe reads from the program's stdout, and writing to the pipe writes to the program's stdin.
For example,
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN880"
></A
><PRE
CLASS="SCREEN"
>$ nickle
&#62; string[*] args = {"-a"};
&#62; file ls = File::pipe ( "ls", args, "r" );
&#62; do printf ( "%s\n", File::fgets ( ls ) );
+ while ( ! File::end ( ls ) );
bin
man
nickle
share</PRE
><P
></P
></DIV
></P
><P
>When a file is no longer needed, it should be closed.</P
><PRE
CLASS="SYNOPSIS"
>void close(file f)</PRE
><DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN884"
></A
><PRE
CLASS="SCREEN"
>&#62; File::close ( ls );</PRE
><P
></P
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN886"
>4.1.2. Flush</A
></H2
><P
>Output written to a file is not immediately written, but buffered until an appropriate time.
Ordinarily, this is not noticed; if, however, it is important to know that all buffers have been written to a file, they can be flushed:</P
><PRE
CLASS="SYNOPSIS"
>void flush (file f)</PRE
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN890"
>4.1.3. End</A
></H2
><P
>Returns true if the file is at end-of-file, otherwise returns false.</P
><PRE
CLASS="SYNOPSIS"
>bool end (file f)</PRE
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN894"
>4.1.4. Characters and strings</A
></H2
><P
>Individual characters can be read and written using <VAR
CLASS="LITERAL"
>getc</VAR
>, <VAR
CLASS="LITERAL"
>getchar</VAR
>, <VAR
CLASS="LITERAL"
>putc</VAR
>, and <VAR
CLASS="LITERAL"
>putchar</VAR
>.</P
><PRE
CLASS="SYNOPSIS"
>int getc(file f)</PRE
><PRE
CLASS="SYNOPSIS"
>int getchar()</PRE
><PRE
CLASS="SYNOPSIS"
>int putc(int c,file f)</PRE
><PRE
CLASS="SYNOPSIS"
>void putchar(int c)</PRE
><P
>A character can be pushed back onto the stream with <VAR
CLASS="LITERAL"
>ungetc</VAR
> or <VAR
CLASS="LITERAL"
>ungetchar</VAR
>.</P
><PRE
CLASS="SYNOPSIS"
>int ungetc(int c, file f)</PRE
><PRE
CLASS="SYNOPSIS"
>int ungetchar(int c)</PRE
><P
>Strings can be read, a line at a time, using <VAR
CLASS="LITERAL"
>fgets</VAR
> and <VAR
CLASS="LITERAL"
>gets</VAR
>.</P
><PRE
CLASS="SYNOPSIS"
>string fgets(file f)</PRE
><PRE
CLASS="SYNOPSIS"
>string gets()</PRE
><P
>All of these are like their C counterparts, with the exception noted in the following section.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN916"
>4.1.5. Unicode and characters vs. bytes</A
></H2
><P
>Unicode is a standard for representing characters, like ASCII.
However, Unicode is designed to be able to support a much larger range of characters; in fact, every character in every alphabet worldwide.
It is optimized so standard ASCII characters retain their ASCII codes, and characters are not larger than they have to be.
Because of its advantages, and the possibility that it may become more standard than ASCII, and because there is no reason not to, Nickle reads and writes Unicode.
This is entirely transparent to the user/programmer.</P
><P
>However, there is one situation in which the programmer will notice (disregarding the case where the programmer finds himself typing on a Sanskrit keyboard): extended characters that do not stand for themselves the same in ASCII and Unicode are <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
> one byte long; they can be as many as four for the really obscure characters.
Therefore, unlike in C, <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>characters cannot be counted on to be the same as bytes</I
></SPAN
>.
For this reason, Nickle provides the following functions:</P
><PRE
CLASS="SYNOPSIS"
>int putb(int c,file f)</PRE
><PRE
CLASS="SYNOPSIS"
>int getb(file f)</PRE
><PRE
CLASS="SYNOPSIS"
>int ungetb(file f)</PRE
><P
>These operate the same as <VAR
CLASS="LITERAL"
>putc</VAR
>, <VAR
CLASS="LITERAL"
>getc</VAR
>, and <VAR
CLASS="LITERAL"
>ungetc</VAR
>, but will always read or write one byte at a time, regardless of character representation.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN929"
>4.1.6. Formatted I/O</A
></H2
><P
>Nickle provides functions such as <VAR
CLASS="LITERAL"
>printf</VAR
>, <VAR
CLASS="LITERAL"
>sprintf</VAR
>, and <VAR
CLASS="LITERAL"
>scanf</VAR
> to perform formatted input and output.
These functions perform like their C counterparts, with the following exceptions:</P
><P
></P
><UL
><LI
><P
>The precision of a field in the format string may be specified to be '-', which means infinite precision.</P
></LI
><LI
><P
>The %g format specifier requires a number, and prints it in the best way possible. For example:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN940"
></A
><PRE
CLASS="SCREEN"
>&#62; printf("%g %g %g\n", 1, 1/3, sqrt(2));
1 0.{3} 1.414213562373095</PRE
><P
></P
></DIV
></P
></LI
><LI
><P
>The %v format specifier will attempt to find the best way to print whatever value it is given.
This is a great way to print polys whose types will not be known ahead of time.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN944"
></A
><PRE
CLASS="SCREEN"
>&#62; printf("%v %v %v\n", 1/3, "hello", fork 4!);
(1/3) "hello" %38</PRE
><P
></P
></DIV
>
Notice that it can even figure out difficult things like the thread returned by 'fork'.</P
></LI
></UL
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN946"
>4.1.7. At the top level</A
></H2
><P
>Many functions in the File namespace have counterparts builtin at the top level; these do not need to be imported from File because they are automatically present.</P
><P
></P
><UL
><LI
><P
>int printf(string fmt, poly args...) is the same as <VAR
CLASS="LITERAL"
>File::printf</VAR
>.</P
></LI
><LI
><P
>string printf(string fmt, poly args...) is the same as <VAR
CLASS="LITERAL"
>File::sprintf</VAR
>.</P
></LI
><LI
><P
>void putchar(int c) is the same as <VAR
CLASS="LITERAL"
>File::putchar</VAR
>.</P
></LI
></UL
><P
>File also contains a namespace called FileGlobals, which is automatically imported.
It contains the following definitions:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN960"
></A
><PRE
CLASS="SCREEN"
>public int scanf (string format, *poly args...)
{
	return File::vfscanf (stdin, format, args);
}

public int vscanf (string format, (*poly)[*] args)
{
	return File::vfscanf (stdin, format, args);
}    

public string gets ()
{
	return File::fgets (stdin);
}

public int getchar ()
{
	return File::getc (stdin);
}

public void ungetchar (int ch)
{
	File::ungetc (ch, stdin);
}</PRE
><P
></P
></DIV
>
Thus, <VAR
CLASS="LITERAL"
>scanf</VAR
>, <VAR
CLASS="LITERAL"
>vscanf</VAR
>, <VAR
CLASS="LITERAL"
>gets</VAR
>, <VAR
CLASS="LITERAL"
>getchar</VAR
>, and <VAR
CLASS="LITERAL"
>ungetchar</VAR
> call the appropriate functions in File and return their results.
The other functions in File must be imported as normal.</P
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x818.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x967.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Nickle Functions</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Math</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
--- NEW FILE: x1012.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Strings</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Nickle Tutorial"
HREF="index.html"><LINK
REL="UP"
TITLE="Builtins"
HREF="c840.html"><LINK
REL="PREVIOUS"
TITLE="Math"
HREF="x967.html"><LINK
REL="NEXT"
TITLE="Advanced topics"
HREF="c1075.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Nickle Tutorial</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x967.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 4. Builtins</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="c1075.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN1012"
>4.3. Strings</A
></H1
><P
>Unlike in C, strings in Nickle are not arrays of or pointers to individual characters.
Consistent with its pattern of providing primitive datatypes for types for things that make sense (e.g. <VAR
CLASS="LITERAL"
>file</VAR
> instead of integer file handles), Nickle provides the <VAR
CLASS="LITERAL"
>string</VAR
> type.
This has several interesting differences from C-style strings:</P
><P
></P
><UL
><LI
><P
>In Nickle, strings are immutable--individual characters may not be changed.</P
></LI
><LI
><P
>Strings are, as with everything else, assigned and passed by-value.
See the section on Copy semantics for details.</P
></LI
></UL
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN1022"
>4.3.1. Operators</A
></H2
><P
>Two useful operators have been overloaded to allow sane manipulation of strings: '+' and array subscript ('[]').</P
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1025"
>4.3.1.1. Subscripting</A
></H3
><P
>Although they are not arrays of characters, it is often useful to access a string a character at a time; the array subscript operator has been overloaded to allow this.
For example:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1028"
></A
><PRE
CLASS="SCREEN"
>&#62; string s = "hello, world";
&#62; s[0]
104
&#62; s[1]
101
&#62; s[2]
108
&#62; s[3]
108
&#62; s[4]
111
&#62; </PRE
><P
></P
></DIV
></P
><P
>Those are the integer representations of each character; they are most likely in ASCII, but not necessarily--see the section on Unicode in the I/O section.
The String namespace provides <VAR
CLASS="LITERAL"
>new</VAR
> to recreate a string from these integer character representations, regardless of ASCII or Unicode:</P
><PRE
CLASS="SYNOPSIS"
>string new(int c)</PRE
><PRE
CLASS="SYNOPSIS"
>string new(int[*] cv)</PRE
><P
>For instance,
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1035"
></A
><PRE
CLASS="SCREEN"
>&#62; String::new(s[0])
"h"</PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1037"
>4.3.1.2. Concatenation</A
></H3
><P
>On strings, '+' is the concatenation operator.
For example,
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1040"
></A
><PRE
CLASS="SCREEN"
>&#62; string s = "hello", t = "world"; 
&#62; s = s + ", ";
&#62; t += "!";
&#62; s+t
"hello, world!"</PRE
><P
></P
></DIV
></P
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN1042"
>4.3.2. String namespace</A
></H2
><P
>In addition, the String namespace provides several useful functions that facilitate using strings, including the following.</P
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1045"
>4.3.2.1. Length</A
></H3
><PRE
CLASS="SYNOPSIS"
>int length ( string s )</PRE
><P
>Returns the number of characters in <VAR
CLASS="LITERAL"
>s</VAR
>.
For example,
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1050"
></A
><PRE
CLASS="SCREEN"
>$ nickle
&#62; String::length ( "hello, world" ) 
12
&#62; </PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1052"
>4.3.2.2. Index</A
></H3
><PRE
CLASS="SYNOPSIS"
>int index ( string t, string p )</PRE
><PRE
CLASS="SYNOPSIS"
>int rindex ( string t, string p )</PRE
><P
>Returns the index of the first occurence of the substring <VAR
CLASS="LITERAL"
>p</VAR
> in <VAR
CLASS="LITERAL"
>t</VAR
>, or -1 if <VAR
CLASS="LITERAL"
>p</VAR
> is not in <VAR
CLASS="LITERAL"
>t</VAR
>; <VAR
CLASS="LITERAL"
>rindex</VAR
> returns the last occurance instead.
For example,
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1062"
></A
><PRE
CLASS="SCREEN"
>$ nickle
&#62; String::index ( "hello, world", "or" ) 
8
&#62; String::index ( "hello, world", "goodbye" )
-1
&#62; String::rindex ( "hello, world", "o" )
8</PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1064"
>4.3.2.3. Substr</A
></H3
><PRE
CLASS="SYNOPSIS"
>string substr ( string s, int i, int l )</PRE
><P
>Returns the substring of <VAR
CLASS="LITERAL"
>s</VAR
> which begins at index <VAR
CLASS="LITERAL"
>i</VAR
> and is <VAR
CLASS="LITERAL"
>l</VAR
> characters long.
If <VAR
CLASS="LITERAL"
>l</VAR
> is negative, returns the substring of that length which preceeds <VAR
CLASS="LITERAL"
>i</VAR
> instead.
For example,
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1073"
></A
><PRE
CLASS="SCREEN"
>$ nickle
&#62; String::substr ( "hello, world", 8, 2 ) 
"or"
&#62; String::substr ( "hello, world", 8, -4 )
"o, w"
&#62;</PRE
><P
></P
></DIV
></P
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x967.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="c1075.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Math</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c840.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Advanced topics</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
--- NEW FILE: x1133.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Nickle Namespaces</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Nickle Tutorial"
HREF="index.html"><LINK
REL="UP"
TITLE="Advanced topics"
HREF="c1075.html"><LINK
REL="PREVIOUS"
TITLE="Advanced topics"
HREF="c1075.html"><LINK
REL="NEXT"
TITLE="Nickle Exceptions"
HREF="x1218.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Nickle Tutorial</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="c1075.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 5. Advanced topics</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x1218.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN1133"
>5.2. Nickle Namespaces</A
></H1
><P
>Namespaces collect related variable and function names and allow control over visibility.
A number of Nickle builtins are gathered into builtin namespaces that may be used.
The following builtin namespaces have sections in this tutorial:</P
><P
></P
><UL
><LI
><P
>Math - Useful mathematical functions.</P
></LI
><LI
><P
>File - File input/output with the 'file' type.</P
></LI
><LI
><P
>Thread - Concurrent processing.</P
></LI
><LI
><P
>Semaphore and Mutex - Synchronization of threads.</P
></LI
><LI
><P
>String - Useful functions for strings.</P
></LI
></UL
><P
>An example namespace might be declared like this:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1148"
></A
><PRE
CLASS="SCREEN"
>namespace Example {

	int blah = 1;
	public int a = 0;

	int function bar(int a) {
		...
	}
	
	protected int function foo(int a) {
		...
	}

}</PRE
><P
></P
></DIV
></P
><P
>The keyword <VAR
CLASS="LITERAL"
>namespace</VAR
> is followed by the name of the namespace and a list of statements that declare names in the namespace.
The publication of those declarations, e.g. <VAR
CLASS="LITERAL"
>public</VAR
> or <VAR
CLASS="LITERAL"
>protected</VAR
> defines how visible they will be outside the namespace.
The namespace itself may be preceeded by publication information, <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>but this has no bearing on the names within the namespace</I
></SPAN
>; it defines the visibility of the name of the namespace.
If the example above had been declared
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1155"
></A
><PRE
CLASS="SCREEN"
>protected namespace Example {
	...
}</PRE
><P
></P
></DIV
>
Then the names within <VAR
CLASS="LITERAL"
>Example</VAR
> would have the same visibility as always, but <VAR
CLASS="LITERAL"
>Example</VAR
> itself would be protected in whatever namespace it belongs to.
In this case, it belongs to the top-level namespace, but namespaces can be nested within each other, which makes the visibility of their own names important.</P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN1159"
>5.2.1. Extend</A
></H2
><PRE
CLASS="SYNOPSIS"
>extend namespace <VAR
CLASS="REPLACEABLE"
>name</VAR
> { <VAR
CLASS="REPLACEABLE"
>statement-list</VAR
> }</PRE
><P
>Names may be added to a namespace after it is initially defined with the <VAR
CLASS="LITERAL"
>extend</VAR
> command.
The namespace <VAR
CLASS="LITERAL"
>name</VAR
> is reopened and the new <VAR
CLASS="LITERAL"
>statement-list</VAR
> is added to the previous ones.
For example,
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1168"
></A
><PRE
CLASS="SCREEN"
>extend namespace Example {
	string[*] greeting = [2]{ "hello", "world" };
}</PRE
><P
></P
></DIV
></P
><P
>Adds <VAR
CLASS="LITERAL"
>greeting</VAR
> to the names already defined in <VAR
CLASS="LITERAL"
>Example</VAR
>.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN1173"
>5.2.2. Peering inside</A
></H2
><PRE
CLASS="SYNOPSIS"
><VAR
CLASS="REPLACEABLE"
>namespace</VAR
>::<VAR
CLASS="REPLACEABLE"
>name</VAR
></PRE
><PRE
CLASS="SYNOPSIS"
>import <VAR
CLASS="REPLACEABLE"
>namespace</VAR
></PRE
><P
>The <VAR
CLASS="LITERAL"
>::</VAR
> operator refers to a <VAR
CLASS="REPLACEABLE"
>name</VAR
>, which is in <VAR
CLASS="REPLACEABLE"
>namespace</VAR
>, analogously to a structure dereference.
If <VAR
CLASS="REPLACEABLE"
>name</VAR
> also refers to a namespace, its names too are visible this way.
Either <VAR
CLASS="LITERAL"
>protected</VAR
> or <VAR
CLASS="LITERAL"
>public</VAR
> names are visible in this way.</P
><P
>An <VAR
CLASS="LITERAL"
>import</VAR
> statement brings all the public names in <VAR
CLASS="REPLACEABLE"
>namespace</VAR
> into scope, overshadowing conflicting names.
Thereafter, those names may be used normally.</P
><P
>A variable is declared with one of three visibilities that defines how it is visible outside its namespace:</P
><P
></P
><UL
><LI
><P
>"public" may be seen outside with <VAR
CLASS="LITERAL"
>::</VAR
> or imported</P
></LI
><LI
><P
>"protected" may be seen outside with <VAR
CLASS="LITERAL"
>::</VAR
> but not imported</P
></LI
><LI
><P
>if neither is specified, it may not be seen outside at all</P
></LI
></UL
><P
>Thus, in our example namespace <VAR
CLASS="LITERAL"
>Example</VAR
>:</P
><P
></P
><UL
><LI
><P
><VAR
CLASS="LITERAL"
>blah</VAR
>, <VAR
CLASS="LITERAL"
>bar</VAR
>, and <VAR
CLASS="LITERAL"
>greeting</VAR
> have no visibility specified and may only be used inside <VAR
CLASS="LITERAL"
>Example</VAR
>.</P
></LI
><LI
><P
>both <VAR
CLASS="LITERAL"
>a</VAR
> (which is public) and <VAR
CLASS="LITERAL"
>foo</VAR
> (which is protected) may be seen with <VAR
CLASS="LITERAL"
>::</VAR
>.</P
></LI
><LI
><P
>an <VAR
CLASS="LITERAL"
>import</VAR
> will only bring <VAR
CLASS="LITERAL"
>a</VAR
> into scope, as it is the only name that is public.</P
></LI
></UL
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="c1075.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x1218.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Advanced topics</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c1075.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Nickle Exceptions</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
--- NEW FILE: x1218.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Nickle Exceptions</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Nickle Tutorial"
HREF="index.html"><LINK
REL="UP"
TITLE="Advanced topics"
HREF="c1075.html"><LINK
REL="PREVIOUS"
TITLE="Nickle Namespaces"
HREF="x1133.html"><LINK
REL="NEXT"
TITLE="Threads and Mutual Exclusion in Nickle"
HREF="x1308.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Nickle Tutorial</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x1133.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 5. Advanced topics</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x1308.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN1218"
>5.3. Nickle Exceptions</A
></H1
><P
>Nickle has first-class exceptions for error handling and quick escapes from recursive algorithms.
A number of exceptions are builtin to Nickle that it throws for various errors, including:</P
><P
></P
><UL
><LI
><P
>exception uninitialized_value(string msg) - Attempt to use an uninitialized value.</P
></LI
><LI
><P
>exception invalid_argument(string msg,int arg,poly val) - The <VAR
CLASS="LITERAL"
>arg</VAR
>th argument to a builtin function had invalid value <VAR
CLASS="LITERAL"
>val</VAR
>.</P
></LI
><LI
><P
>exception readonly_box(string msg,poly val) - Attempt to change the value of a read-only quantity to <VAR
CLASS="LITERAL"
>val</VAR
>.</P
></LI
><LI
><P
>exception invalid_array_bounds(string msg,poly a,poly i) - Attempt to access array <VAR
CLASS="LITERAL"
>a</VAR
> at index <VAR
CLASS="LITERAL"
>i</VAR
> is out of bounds.</P
></LI
><LI
><P
>exception divide_by_zero(string msg,real num,real den) - Attempt to divide <VAR
CLASS="LITERAL"
>num</VAR
> by <VAR
CLASS="LITERAL"
>den</VAR
> when <VAR
CLASS="LITERAL"
>den</VAR
> is zero.</P
></LI
><LI
><P
>exception invalid_struct_member(string msg,poly struct,string name) - Attempt to refer to member <VAR
CLASS="LITERAL"
>name</VAR
> of the object <VAR
CLASS="LITERAL"
>struct</VAR
>, which does not exist.</P
></LI
><LI
><P
>exception invalid_binop_values(string msg,poly arg1,poly arg2) - Attempt to evaluate a binary operator with arguments <VAR
CLASS="LITERAL"
>arg1</VAR
> and <VAR
CLASS="LITERAL"
>arg2</VAR
>, where at least one of these values is invalid.</P
></LI
><LI
><P
>exception invalid_unop_values(string msg,poly arg) - Attempt to evaluate a unary operator with invalid argument <VAR
CLASS="LITERAL"
>arg</VAR
>.</P
></LI
></UL
><P
>The following syntax may be used to declare a new exception:</P
><PRE
CLASS="SYNOPSIS"
>exception <VAR
CLASS="REPLACEABLE"
>name</VAR
> ( <VAR
CLASS="REPLACEABLE"
>type</VAR
> <VAR
CLASS="REPLACEABLE"
>name</VAR
>, ... )</PRE
><P
>For example,
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1257"
></A
><PRE
CLASS="SCREEN"
>exception my_exception ( string msg, int a, int b, int c );</PRE
><P
></P
></DIV
></P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN1259"
>5.3.1. Raise</A
></H2
><PRE
CLASS="SYNOPSIS"
>raise <VAR
CLASS="REPLACEABLE"
>name</VAR
> ( <VAR
CLASS="REPLACEABLE"
>value</VAR
>, ... )</PRE
><P
>Raises the named exception with the given arguments, e.g.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1265"
></A
><PRE
CLASS="SCREEN"
>raise my_exception ( "message", 0, 1, 2 );</PRE
><P
></P
></DIV
></P
><P
>Execution is broken and <VAR
CLASS="LITERAL"
>my_exception</VAR
> travels up the stack until it is caught by a try-catch block or it reaches the top level, where it prints an error message such as:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1269"
></A
><PRE
CLASS="SCREEN"
>Unhandled exception "my_exception"
	3
	2
	1
	"message"</PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN1271"
>5.3.2. Try - catch</A
></H2
><PRE
CLASS="SYNOPSIS"
>try <VAR
CLASS="REPLACEABLE"
>statement</VAR
></PRE
><PRE
CLASS="SYNOPSIS"
>catch <VAR
CLASS="REPLACEABLE"
>name</VAR
> ( <VAR
CLASS="REPLACEABLE"
>type</VAR
> <VAR
CLASS="REPLACEABLE"
>name</VAR
>, ... ) { <VAR
CLASS="REPLACEABLE"
>statement-list</VAR
> }</PRE
><P
><VAR
CLASS="LITERAL"
>try</VAR
> executes <VAR
CLASS="REPLACEABLE"
>statement</VAR
>; if it raises an exception whose name matches that of a succeeding <VAR
CLASS="LITERAL"
>catch</VAR
> block, the arguments are placed in the names specified and the associated <VAR
CLASS="REPLACEABLE"
>statement-list</VAR
> is executed.
Control continues after the catch without continuing up the stack; if further propagation is desired, <VAR
CLASS="REPLACEABLE"
>statement-list</VAR
> should re-raise the exception.
Any number of catch blocks may be associated with a try statement.
For example:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1286"
></A
><PRE
CLASS="SCREEN"
>exception my_exception(string msg,int a,int b,int c);

try raise my_exception("blah",1,2,3);
catch my_exception(string msg,int a,int b,int c) {
	printf("%s: exception successfully caught (%d,%d,%d).\n",msg,a,b,c);
}</PRE
><P
></P
></DIV
></P
><P
>This example tries to execute a function that raises an exception; since that exception matches the catch block, "blah", 1, 2, and 3 (the arguments) are put into <VAR
CLASS="LITERAL"
>msg</VAR
>, <VAR
CLASS="LITERAL"
>a</VAR
>, <VAR
CLASS="LITERAL"
>b</VAR
>, and <VAR
CLASS="LITERAL"
>c</VAR
> and the statement list is executed, which in this case merely prints out the arguments received and continues:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1293"
></A
><PRE
CLASS="SCREEN"
>blah: exception successfully caught (1,2,3).</PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN1295"
>5.3.3. Twixt</A
></H2
><P
>Nickle does not provide a <VAR
CLASS="LITERAL"
>finally</VAR
> clause to a <VAR
CLASS="LITERAL"
>try-catch</VAR
>.
In order to ensure the order of some expressions, it provides <VAR
CLASS="LITERAL"
>twixt</VAR
> (See the section on Statements).
For example,
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1301"
></A
><PRE
CLASS="SCREEN"
>exception my_exception(string msg, int a, int b, int c);

void foo(string msg, int a, int b, int c) {
	twixt(printf("entering twixt..."); printf("leaving twixt.\n"))
		raise my_exception(msg, a, b, c);
}

try foo("blah", 1, 2, 3);
catch my_exception(string msg,int a,int b,int c) {
printf("%s: exception successfully caught (%d,%d,%d).\n",msg,a,b,c);
}</PRE
><P
></P
></DIV
>
Will produce the output:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1303"
></A
><PRE
CLASS="SCREEN"
>entering twixt...leaving twixt.
blah: exception successfully caught (1,2,3).</PRE
><P
></P
></DIV
></P
><P
>Notice the order of the printed messages: <VAR
CLASS="LITERAL"
>twixt</VAR
> finished up before the exception was handled by the <VAR
CLASS="LITERAL"
>catch</VAR
>.
This is an elegant way to accomplish something that should be done finally, in this case printing the message "leaving twixt" for demonstration.</P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x1133.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x1308.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Nickle Namespaces</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c1075.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Threads and Mutual Exclusion in Nickle</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
--- NEW FILE: x1308.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Threads and Mutual Exclusion in Nickle</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Nickle Tutorial"
HREF="index.html"><LINK
REL="UP"
TITLE="Advanced topics"
HREF="c1075.html"><LINK
REL="PREVIOUS"
TITLE="Nickle Exceptions"
HREF="x1218.html"><LINK
REL="NEXT"
TITLE="Nickle Continuations"
HREF="x1436.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Nickle Tutorial</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x1218.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 5. Advanced topics</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x1436.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN1308"
>5.4. Threads and Mutual Exclusion in Nickle</A
></H1
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN1310"
>5.4.1. Basic threading</A
></H2
><P
>Threads provide concurrent processing of calculations.
They are created with the <VAR
CLASS="LITERAL"
>fork</VAR
> operator, which spawns a child thread to evaluate its argument and returns it as a variable of the first-class type <VAR
CLASS="LITERAL"
>thread</VAR
>:</P
><PRE
CLASS="SYNOPSIS"
>fork <VAR
CLASS="LITERAL"
>expr</VAR
></PRE
><P
>The thread it returns is typically stored like this:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1318"
></A
><PRE
CLASS="SCREEN"
>thread t = fork x!;</PRE
><P
></P
></DIV
></P
><P
>In the above example, <VAR
CLASS="LITERAL"
>fork</VAR
> immediately returns a thread, which is stored in <VAR
CLASS="LITERAL"
>t</VAR
>.
That thread will calculate the factorial of <VAR
CLASS="LITERAL"
>x</VAR
> in the background while the program continues; when the calculation is finished it will block and wait for the parent thread (the one that forked it) to kill, join, or otherwise recognize it.</P
><P
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>Threads share names</I
></SPAN
>; if a thread changes the value of a variable, that change will occur in the other threads as well.
See Mutual exclusion below.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN1326"
>5.4.2. Thread functions</A
></H2
><P
>The builtin namespace Thread has functions for manipulating threads once they have been forked.</P
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1329"
>5.4.2.1. Kill</A
></H3
><PRE
CLASS="SYNOPSIS"
>int kill ( thread t, ... )</PRE
><P
>Kills the threads it takes as arguments, regardless of whether or not they are finished, and returns the number of threads successfully killed. </P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1333"
>5.4.2.2. Join</A
></H3
><PRE
CLASS="SYNOPSIS"
>poly join ( thread t )</PRE
><P
>Waits for thread <VAR
CLASS="LITERAL"
>t</VAR
> to finish and returns the value of its expression.
This is how to get the value back out of a thread.
Once joined, the thread will dissappear.
For example,
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1338"
></A
><PRE
CLASS="SCREEN"
>thread t = fork 1000!;
# something else...
printf("1000! = %d\n",Thread::join(t));</PRE
><P
></P
></DIV
></P
><P
>will execute 'something else' while <VAR
CLASS="LITERAL"
>t</VAR
> runs, then wait for it to complete and print out its value.</P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1342"
>5.4.2.3. Current</A
></H3
><PRE
CLASS="SYNOPSIS"
>thread current ( )</PRE
><P
>Returns the currently running thread. Note that things such as <VAR
CLASS="LITERAL"
>kill(current())</VAR
> and <VAR
CLASS="LITERAL"
>join(current())</VAR
> are allowed, although the former merely exits and the latter hangs forever; watch out for these errors.</P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1348"
>5.4.2.4. Priorities</A
></H3
><PRE
CLASS="SYNOPSIS"
>int set_priority ( thread t, int i )</PRE
><PRE
CLASS="SYNOPSIS"
>int get_priority ( thread t )</PRE
><P
>Priorities determine how runtime is divided among threads; a thread with higher priority will always run before one with a lower priority.
<VAR
CLASS="LITERAL"
>set_priority</VAR
> sets the priority of <VAR
CLASS="LITERAL"
>t</VAR
> to <VAR
CLASS="LITERAL"
>i</VAR
> and returns the new priority.
<VAR
CLASS="LITERAL"
>get_priority</VAR
> returns the priority of thread <VAR
CLASS="LITERAL"
>t</VAR
>.</P
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN1358"
>5.4.3. Mutual exclusion</A
></H2
><P
>Consider the following situation:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1361"
></A
><PRE
CLASS="SCREEN"
>import Thread;

void function para() {
	printf("My next statement will be false.\n");
}

void function dox() {
	printf("My previous statement was true.\n");
}

thread t = fork para();
thread s = fork dox();
join(t);
join(s);</PRE
><P
></P
></DIV
></P
><P
>When run, this prints out the less than clear message
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1364"
></A
><PRE
CLASS="SCREEN"
>MMyy  nperxetv isotuast esmteantte mweinltl  wbaes  ftarlusee..</PRE
><P
></P
></DIV
></P
><P
>Why? Because the two threads are running simultaneously and take turns printing out their messages; the result is that they are interleaved unreadably.
The solution is in the builtin namespace Mutex.
A mutex is a first-class object which threads can use to coordinate conflicting sections of code.
Mutex defines the following functions:</P
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1367"
>5.4.3.1. New</A
></H3
><PRE
CLASS="SYNOPSIS"
>mutex new ( )</PRE
><P
>Creates a new mutex and returns it.</P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1371"
>5.4.3.2. Acquire</A
></H3
><PRE
CLASS="SYNOPSIS"
>bool acquire ( mutex m )</PRE
><P
><VAR
CLASS="LITERAL"
>acquire</VAR
> blocks until <VAR
CLASS="LITERAL"
>m</VAR
> is free, then locks it and returns true.
At the top of the conflicting code, each thread should acquire the mutex; since only one at a time can have it, they will take turns executing.
There is also a <VAR
CLASS="LITERAL"
>try_acquire</VAR
> that returns false immediately rather than blocking if <VAR
CLASS="LITERAL"
>m</VAR
> is in use, but it is deprecated.</P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1379"
>5.4.3.3. Release</A
></H3
><PRE
CLASS="SYNOPSIS"
>void release ( mutex m )</PRE
><P
>When the thread which owns a mutex leaves the conflicting section of code, it should call release to free it for the next thread to acquire it.</P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1383"
>5.4.3.4. Owner</A
></H3
><PRE
CLASS="SYNOPSIS"
>mutex_owner owner ( mutex m )</PRE
><P
>Returns the owner of <VAR
CLASS="LITERAL"
>m</VAR
>: either the thread which currently owns it or null if it is free.</P
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN1388"
>5.4.4. An example</A
></H2
><P
>This is how the example above might work with mutual exclusion:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1391"
></A
><PRE
CLASS="SCREEN"
>import Mutex;
import Thread;

mutex m = new();

void function para() {
	acquire(m);
	printf("My next statement will be false.\n");
	release(m);
}

void function dox() {
	acquire(m);
	printf("My previous statement was true.\n");
	release(m);
}

thread t = fork para();
thread s = fork dox();
join(t);
	join(s);</PRE
><P
></P
></DIV
></P
><P
>This prints out, as expected,
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1394"
></A
><PRE
CLASS="SCREEN"
>My next statement will be false.
My previous statement was true.</PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN1396"
>5.4.5. Semaphores</A
></H2
><P
>Nickle also has counting semaphores, implemented in the Semaphore namespace.
Semaphores are similar to mutexes, but have some number of threads that may run that isn't necessarily one, as it is with mutexes.
A semaphore with a count of one behaves just like a mutex.</P
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1399"
>5.4.5.1. New</A
></H3
><P
>Semaphores are created with <VAR
CLASS="LITERAL"
>new</VAR
>, which is unlike <VAR
CLASS="LITERAL"
>Mutex::new</VAR
> in that it takes an argument: the number of threads it will run simultaneously.</P
><PRE
CLASS="SYNOPSIS"
>semaphore new ( int c )</PRE
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1405"
>5.4.5.2. Wait and Signal</A
></H3
><P
>Just as Mutexes are <VAR
CLASS="LITERAL"
>acquire</VAR
>d and <VAR
CLASS="LITERAL"
>release</VAR
>d, threads <VAR
CLASS="LITERAL"
>wait</VAR
> on semaphores and <VAR
CLASS="LITERAL"
>signal</VAR
> them when finished.</P
><PRE
CLASS="SYNOPSIS"
>void wait ( semaphore s )</PRE
><PRE
CLASS="SYNOPSIS"
>void signal ( semaphore s )</PRE
><P
><VAR
CLASS="LITERAL"
>wait</VAR
> merely decrements the count of <VAR
CLASS="LITERAL"
>s</VAR
>, which starts with the initial value specified by <VAR
CLASS="LITERAL"
>new</VAR
>.
If the count, after the decrement, is positive, the thread continues to run; if it is negative, it blocks until the count becomes positive again.
This will occur when one of the running threads calls <VAR
CLASS="LITERAL"
>signal</VAR
>, which increments the count of <VAR
CLASS="LITERAL"
>s</VAR
> and wakes up another thread if any are waiting.</P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1420"
>5.4.5.3. Negative initial counts</A
></H3
><P
>If <VAR
CLASS="LITERAL"
>new</VAR
> is called with a negative initial count, much of the meaning of the semaphore is inverted.
The count now refers to the number of threads that must wait until one can execute; that is, the first <VAR
CLASS="LITERAL"
>c</VAR
> threads will block, and the <VAR
CLASS="LITERAL"
>c</VAR
>+1th will execute.</P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1426"
>5.4.5.4. Why semaphores?</A
></H3
><P
>Semaphores are useful in situations where several threads can run simultaneously, but not more than a certain number.
They would be great, for instance, to work in a licensing system, where each thread needs some command, but only a certain number may run at a given time.</P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1429"
>5.4.5.5. Be careful</A
></H3
><P
>Semaphores, unlike mutexes, are very error-prone.
They are not <VAR
CLASS="LITERAL"
>owned</VAR
>, in the sense that mutexes are, and therefore do not check what threads are signalling or waiting on them.
Thus, situations like this are possible:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1433"
></A
><PRE
CLASS="SCREEN"
>&#62; import Semaphore;
&#62; semaphore s = new(3);
&#62; s  
semaphore 1 (3);
&#62; wait(s);       
&#62; s
semaphore 1 (2);
&#62; wait(s);
&#62; s
semaphore 1 (1);
&#62; s
semaphore 1 (1)
&#62; for(int i=0; i &#60; 100; ++i)
+   signal(s);
&#62; s
semaphore 1 (101)
&#62; wait(s)
&#62; s
semaphore 1 (100)
&#62; </PRE
><P
></P
></DIV
></P
><P
>Therefore, code must be written carefully so that threads do not signal the semaphore more than once, and only once they have waited on it.</P
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x1218.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x1436.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Nickle Exceptions</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c1075.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Nickle Continuations</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
--- NEW FILE: x1436.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Nickle Continuations</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Nickle Tutorial"
HREF="index.html"><LINK
REL="UP"
TITLE="Advanced topics"
HREF="c1075.html"><LINK
REL="PREVIOUS"
TITLE="Threads and Mutual Exclusion in Nickle"
HREF="x1308.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Nickle Tutorial</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x1308.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 5. Advanced topics</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
>&nbsp;</TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN1436"
>5.5. Nickle Continuations</A
></H1
><P
>Arbitrary flow control is accomplished in Nickle with first-class continuations and the functions <VAR
CLASS="LITERAL"
>setjmp</VAR
> and <VAR
CLASS="LITERAL"
>longjmp</VAR
>.
These are similar to those in C, but without restrictions on the target.</P
><PRE
CLASS="SYNOPSIS"
>poly setjmp ( continuation *c, poly retval )</PRE
><PRE
CLASS="SYNOPSIS"
>void lomgjmp ( continuation c, poly retval )</PRE
><P
>Setjmp saves the state of the program, including program counter and names in scope, in <VAR
CLASS="LITERAL"
>c</VAR
> and returns <VAR
CLASS="LITERAL"
>retval</VAR
>.</P
><P
>Longjmp returns <VAR
CLASS="LITERAL"
>retval</VAR
> <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>from the setjmp that set <VAR
CLASS="LITERAL"
>c</VAR
></I
></SPAN
>.
There can be two distinctions from this jump and the initial call to setjmp: the return value may differ, and variables that have changed retain their new values.</P
><P
>Continuations are often used to implement control structures that do not exist in the language, interpreters, and escaping from recursive algorithms.
For example, the following is a simple binary tree search that uses continuations to jump directly to the top instead of returning up each branch once the result is found.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1451"
></A
><PRE
CLASS="SCREEN"
>typedef tree;

typedef struct {
	int key;
	poly data;
	&#38;poly left, right;
} tree;

void function search ( tree t, int i, &#38;continuation c ) {
	if ( i &#60; t.key &#38;&#38; ! is_void ( t.left ) )
		search ( t.left, i, &#38;c );
	else if ( i &#62; t.key &#38;&#38; ! is_void ( t.right ) )
		search ( t.right, i, &#38;c );
	else if ( i == t.key )
		longjmp ( c, t.data );
}

tree t = { key = 2, data = "blah", left = reference ( &#60;&#62; ), right = reference ( &#60;&#62; ) };

continuation c;
int i = 0;
{
	poly p = setjmp ( &#38;c, &#60;&#62; );
	++i;
	printf ( "I have been here %d times.\n", i );

	if ( is_void ( p ) )
		search ( t, 2, &#38;c );
	else
		printf ( "value = %g\n", p );
}</PRE
><P
></P
></DIV
></P
><P
>This is a pretty normal binary tree search, but notice how it is run: a continuation is set; if setjmp returns &lt;&gt; (which it will the first time), a value is searched for (this is a pretty degenerate example with only one node).
If an actual value is returned, it must be from the longjmp in search, and the value is printed; a message is printed to emphasize that setjmp returns twice.
This optimizes the return from what can be a very deeply nested search.</P
><P
>Notice that the last part of the code is inside curly braces.
This is legal, of course, but ordinarily not very useful.
It is used in this case to get around a slight flaw in Nickle: currently, each top-level command is executed in its own thread.
Thus when longjmp tries to return from the setjmp, that thread has already finished and the program exits.
By placing those statements in curly braces, they will all be executed in the same thread and setjmp will still exist for longjmp to find.</P
><P
>This sort of escape from a nested search is also commonly done with exceptions, raising one when the value is found and catching it at the top, passing the value as an argument to the exception.
Actually, that method is more common because of its simplicity, but this example was done using continuations to demonstrate them.</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x1308.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>&nbsp;</TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Threads and Mutual Exclusion in Nickle</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c1075.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>&nbsp;</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
--- NEW FILE: x368.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Nickle Expressions</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Nickle Tutorial"
HREF="index.html"><LINK
REL="UP"
TITLE="Language introduction"
HREF="c131.html"><LINK
REL="PREVIOUS"
TITLE="Language introduction"
HREF="c131.html"><LINK
REL="NEXT"
TITLE="Control Statements in Nickle"
[...1063 lines suppressed...]
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c131.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Control Statements in Nickle</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
--- NEW FILE: x655.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Control Statements in Nickle</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Nickle Tutorial"
HREF="index.html"><LINK
REL="UP"
TITLE="Language introduction"
HREF="c131.html"><LINK
REL="PREVIOUS"
TITLE="Nickle Expressions"
HREF="x368.html"><LINK
REL="NEXT"
TITLE="Nickle Functions"
HREF="x818.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Nickle Tutorial</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x368.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 3. Language introduction</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x818.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN655"
>3.3. Control Statements in Nickle</A
></H1
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN657"
>3.3.1. Simple statements</A
></H2
><PRE
CLASS="SYNOPSIS"
><VAR
CLASS="REPLACEABLE"
>expr</VAR
>;</PRE
><PRE
CLASS="SYNOPSIS"
>;</PRE
><PRE
CLASS="SYNOPSIS"
>{ <VAR
CLASS="REPLACEABLE"
>statement</VAR
> ... }</PRE
><P
>The simplest statement is merely an expression terminated by a semicolon; the expression is evaluated.
A semicolon by itself is allowed but does nothing.
One or more statements may be grouped inside curly braces to form one compound statement; each is executed in order.
Any statements may compose the statement list, including control statements and other curly-bracketed lists.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN665"
>3.3.2. Conditionals</A
></H2
><PRE
CLASS="SYNOPSIS"
>if ( <VAR
CLASS="REPLACEABLE"
>expr</VAR
> ) <VAR
CLASS="REPLACEABLE"
>statement</VAR
></PRE
><PRE
CLASS="SYNOPSIS"
>else <VAR
CLASS="REPLACEABLE"
>statement</VAR
></PRE
><P
><VAR
CLASS="LITERAL"
>if</VAR
> is used to execute a section of code only under some condition: If <VAR
CLASS="REPLACEABLE"
>expr</VAR
> is true, <VAR
CLASS="REPLACEABLE"
>statement</VAR
> is executed; otherwise control skips over it.
For example:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN676"
></A
><PRE
CLASS="SCREEN"
>if ( x == 0 )
	printf ( "x is zero.\n" );</PRE
><P
></P
></DIV
></P
><P
>In this case, the message will be printed only if <VAR
CLASS="LITERAL"
>x</VAR
> is zero.</P
><P
><VAR
CLASS="LITERAL"
>else</VAR
> allows for a choice if the condition fails.
It executes its <VAR
CLASS="REPLACEABLE"
>statement</VAR
> if the most recent <VAR
CLASS="LITERAL"
>if</VAR
> or <VAR
CLASS="LITERAL"
>twixt</VAR
> (see below) did not.
For example,
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN685"
></A
><PRE
CLASS="SCREEN"
>if ( x == 0 )
	printf ( "x is zero.\n" );
else
	printf ( "x is not zero.\n" );</PRE
><P
></P
></DIV
></P
><P
>More than one option may be presented by nesting further 'if's in 'else' statements like this:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN688"
></A
><PRE
CLASS="SCREEN"
>if ( x == 0 )
	printf ( "x is zero.\n" );
else if ( x &#60; 0 )
	printf ( "x is negative.\n" );
else
	printf ( "x is positive.\n" );</PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN690"
>3.3.3. Twixt</A
></H2
><PRE
CLASS="SYNOPSIS"
>twixt ( <VAR
CLASS="REPLACEABLE"
>expr</VAR
>; <VAR
CLASS="REPLACEABLE"
>expr</VAR
> ) <VAR
CLASS="REPLACEABLE"
>statement</VAR
></PRE
><P
><VAR
CLASS="LITERAL"
>twixt</VAR
>, like <VAR
CLASS="LITERAL"
>if</VAR
>, executes <VAR
CLASS="REPLACEABLE"
>statement</VAR
> only if the first <VAR
CLASS="REPLACEABLE"
>expr</VAR
> is true.
If <VAR
CLASS="REPLACEABLE"
>statement</VAR
> is executed, the second <VAR
CLASS="REPLACEABLE"
>expr</VAR
> is evaluated after it.
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>That order is gauranteed</I
></SPAN
> - even if <VAR
CLASS="REPLACEABLE"
>statement</VAR
> throws an exception or <VAR
CLASS="LITERAL"
>long_jmp</VAR
>s out of the <VAR
CLASS="LITERAL"
>twixt</VAR
>, the second <VAR
CLASS="REPLACEABLE"
>expr</VAR
> will be evaluated.
Thus, <VAR
CLASS="LITERAL"
>twixt</VAR
> is useful in locked operations where the statement should only be executed if a lock was successfully acquired and the lock must be released afterward:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN709"
></A
><PRE
CLASS="SCREEN"
>twixt ( get_lock ( ); release_lock ( ) )
	locked_operation ( );</PRE
><P
></P
></DIV
></P
><P
>'Else' statements may bind to 'twixt's as well as 'if's, providing choices
such as:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN712"
></A
><PRE
CLASS="SCREEN"
>twixt ( get_lock ( ); release_lock ( ) )
	locked_operation ( );
else
	printf ( "failed to acquire lock.\n" );</PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN714"
>3.3.4. Switch</A
></H2
><PRE
CLASS="SYNOPSIS"
>switch ( <VAR
CLASS="REPLACEABLE"
>expr</VAR
> ) { case <VAR
CLASS="REPLACEABLE"
>expr</VAR
>: <VAR
CLASS="REPLACEABLE"
>statement-list</VAR
> ... default: <VAR
CLASS="REPLACEABLE"
>statement-list</VAR
> }</PRE
><P
>Control jumps to the first <VAR
CLASS="LITERAL"
>case</VAR
> whose <VAR
CLASS="REPLACEABLE"
>expr</VAR
> evaluates to the same value as the <VAR
CLASS="REPLACEABLE"
>expr</VAR
> at the top.
Unlike in C, these values do not have to be integers, or even constant.
The optional case <VAR
CLASS="LITERAL"
>default</VAR
> matches any value.
If nothing is matched and there is no <VAR
CLASS="LITERAL"
>default</VAR
>, control skips the <VAR
CLASS="LITERAL"
>switch</VAR
> entirely.
This example prints out a number to the screen, replacing it by a letter as though it were a poker card:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN728"
></A
><PRE
CLASS="SCREEN"
>switch ( x ) {
	case 1:
		printf ( "A\n" );	/* ace */
		break;
	case 11:
		printf ( "J\n" );	/* jack */
		break;
	case 12:
		printf ( "Q\n" );	/* queen */
		break;
	case 13:
		printf ( "K\n" );	/* king */
		break;
	default:
		printf ( "%d\n", x );	/* numeric */
		break;
}</PRE
><P
></P
></DIV
></P
><P
>Notice the <VAR
CLASS="LITERAL"
>break</VAR
>s in the example.
Once control jumps to the matching case, it continues normally: Upon exhausting that <VAR
CLASS="REPLACEABLE"
>statement-list</VAR
>, <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>it does not jump out of the <VAR
CLASS="LITERAL"
>switch</VAR
></I
></SPAN
>; it continues through the subsequent statement lists.
Here is an example of this 'falling through':
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN735"
></A
><PRE
CLASS="SCREEN"
>int x = 3;

switch ( sign ( x ) ) {
	case -1:
		printf ( "x is negative.\n" );
	case 1:
		printf ( "x is positive.\n" );
	default:
		printf ( "x is zero.\n" );
}</PRE
><P
></P
></DIV
></P
><P
>This prints:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN738"
></A
><PRE
CLASS="SCREEN"
>x is positive.
x is zero.</PRE
><P
></P
></DIV
></P
><P
>Falling through may be desirable if several cases are treated similarly; however, it should be used sparingly and probably commented so it is clear you are doing it on purpose.
This is a difficult error to catch.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN741"
>3.3.5. Union switch</A
></H2
><PRE
CLASS="SYNOPSIS"
>union switch ( <VAR
CLASS="REPLACEABLE"
>union</VAR
> ) { case <VAR
CLASS="REPLACEABLE"
>name</VAR
>: <VAR
CLASS="REPLACEABLE"
>statement-list</VAR
> ... default: <VAR
CLASS="REPLACEABLE"
>statement-list</VAR
> }</PRE
><P
><VAR
CLASS="LITERAL"
>union switch</VAR
> is similar to <VAR
CLASS="LITERAL"
>switch</VAR
>.
It matches its <VAR
CLASS="LITERAL"
>case</VAR
>s based on what name currently applies to the union's value.
As always, <VAR
CLASS="LITERAL"
>default</VAR
> matches everything.
The following example chooses the best way to print the union:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN753"
></A
><PRE
CLASS="SCREEN"
>union {
	int a;
	string b;
} u;

u.b = "hello";

union switch ( u ) {
	case a:
		printf ( "%d\n", u.a );
		break;
	case b:
		printf ( "%s\n", u.b );
		break;
}</PRE
><P
></P
></DIV
></P
><P
>In this case, it prints 'hello'.</P
><P
>An additional name may follow that of a case; the union's value will be available inside the case by that name.
The switch above could have been written:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN757"
></A
><PRE
CLASS="SCREEN"
>union switch ( u ) {
	case a num:
		printf ( "%d\n", num );
		break;
	case b str:
		printf ( "%s\n", str );
		break;
}</PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN759"
>3.3.6. Loops</A
></H2
><PRE
CLASS="SYNOPSIS"
>while ( <VAR
CLASS="REPLACEABLE"
>expr</VAR
> ) <VAR
CLASS="REPLACEABLE"
>statement</VAR
></PRE
><PRE
CLASS="SYNOPSIS"
>do <VAR
CLASS="REPLACEABLE"
>statement</VAR
> while ( <VAR
CLASS="REPLACEABLE"
>expr</VAR
> )</PRE
><PRE
CLASS="SYNOPSIS"
>for ( <VAR
CLASS="REPLACEABLE"
>expr</VAR
>; <VAR
CLASS="REPLACEABLE"
>expr</VAR
>; <VAR
CLASS="REPLACEABLE"
>expr</VAR
> ) <VAR
CLASS="REPLACEABLE"
>statement</VAR
></PRE
><P
><VAR
CLASS="LITERAL"
>while</VAR
> executes <VAR
CLASS="REPLACEABLE"
>statement</VAR
> repeatedly as long as <VAR
CLASS="REPLACEABLE"
>expr</VAR
> is true.
Control continues outside the loop when <VAR
CLASS="REPLACEABLE"
>expression</VAR
> becomes false.
For example:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN777"
></A
><PRE
CLASS="SCREEN"
>int x = 0;
while ( x &#60; 10 ) {
	printf ( "%d\n", x );
	++x;
}</PRE
><P
></P
></DIV
></P
><P
>This prints the numbers from zero to nine.</P
><P
><VAR
CLASS="LITERAL"
>do-while</VAR
> is like <VAR
CLASS="LITERAL"
>while</VAR
>, but tests the condition after each iteration rather than before.
Thus, it is garaunteed to execute at least once.
It is often used in input while testing for end-of-file:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN783"
></A
><PRE
CLASS="SCREEN"
>file f = File::open ( "test", "r" );

do {
	printf ( "%s\n", File::fgets ( f ) );
} while ( ! end ( f ) );

close ( f );</PRE
><P
></P
></DIV
></P
><P
><VAR
CLASS="LITERAL"
>for</VAR
> begins by evaluating the first <VAR
CLASS="REPLACEABLE"
>expr</VAR
>, which often initializes a counter variable; since declarations are expressions in Nickle, they may be used here and the counter will be local to the loop.
Then it executes <VAR
CLASS="REPLACEABLE"
>statement</VAR
> as long as the second <VAR
CLASS="REPLACEABLE"
>expr</VAR
> is true, like <VAR
CLASS="LITERAL"
>while</VAR
>.
After each iteration, the third <VAR
CLASS="REPLACEABLE"
>expr</VAR
> is evaluated, which usually increments or decrements the counter variable.
The <VAR
CLASS="LITERAL"
>while</VAR
> example above can also be written as the following <VAR
CLASS="LITERAL"
>for</VAR
> loop:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN794"
></A
><PRE
CLASS="SCREEN"
>for ( int x = 0; x &#60; 10; ++x )
	printf ( "%d\n", x );</PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN796"
>3.3.7. Flow control</A
></H2
><PRE
CLASS="SYNOPSIS"
>continue</PRE
><PRE
CLASS="SYNOPSIS"
>break</PRE
><PRE
CLASS="SYNOPSIS"
>return <VAR
CLASS="REPLACEABLE"
>expr</VAR
></PRE
><P
><VAR
CLASS="LITERAL"
>continue</VAR
> restarts the nearest surrounding <VAR
CLASS="LITERAL"
>do-while</VAR
>, <VAR
CLASS="LITERAL"
>while</VAR
>, or <VAR
CLASS="LITERAL"
>for</VAR
> loop by jumping directly to the conditional test.
The iterative statement of a <VAR
CLASS="LITERAL"
>for</VAR
> loop will be evaluated first.</P
><P
><VAR
CLASS="LITERAL"
>break</VAR
> leaves the nearest surrounding <VAR
CLASS="LITERAL"
>do-while</VAR
>, <VAR
CLASS="LITERAL"
>while</VAR
>, <VAR
CLASS="LITERAL"
>for</VAR
>, or <VAR
CLASS="LITERAL"
>switch</VAR
> statement by jumping to its end.
The iterative statement of a <VAR
CLASS="LITERAL"
>for</VAR
> loop will not be evaluated.</P
><P
><VAR
CLASS="LITERAL"
>return</VAR
> returns from the nearest surrounding function with value <VAR
CLASS="REPLACEABLE"
>expr</VAR
>.</P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x368.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x818.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Nickle Expressions</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c131.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Nickle Functions</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
--- NEW FILE: x78.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Commands</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Nickle Tutorial"
HREF="index.html"><LINK
REL="UP"
TITLE="Nickle Basics"
HREF="c32.html"><LINK
REL="PREVIOUS"
TITLE="Nickle Basics"
HREF="c32.html"><LINK
REL="NEXT"
TITLE="Language introduction"
HREF="c131.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Nickle Tutorial</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="c32.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 2. Nickle Basics</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="c131.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN78"
>2.2. Commands</A
></H1
><P
>The following are commands that the Nickle interpreter understands, not actual language constructs.
They may be issued only at the top level.</P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN81"
>2.2.1. Expressions</A
></H2
><P
>If an expression is issued at the top level, such as <VAR
CLASS="LITERAL"
>3**4</VAR
> or <VAR
CLASS="LITERAL"
>100!,</VAR
>, its value is printed to standard output.
If the expression ends with a # sign and another expression, its value is printed in whatever base the second expression evaluates to.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN86"
></A
><PRE
CLASS="SCREEN"
>$ nickle
&#62; 10!
3628800
&#62; 3**4
81
&#62; 3**4 # 3
10000
&#62;</PRE
><P
></P
></DIV
></P
><P
>Statements, from expressions terminated by semicolons to complicated control structures, are executed but have no value to print.
Statements are not commands but actual syntax, so they may be used in scripts.
If a line is ended before it can be sensible as an expression or statement, Nickle will continue until it is a statement, e.g.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN89"
></A
><PRE
CLASS="SCREEN"
>$ nickle
&#62; int x
+ = 0
+ ;
&#62;</PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN91"
>2.2.2. Quit</A
></H2
><P
>The <VAR
CLASS="LITERAL"
>quit</VAR
> command exits Nickle.
An optional argument specifies the return value.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN95"
></A
><PRE
CLASS="SCREEN"
>$ nickle
&#62; quit
$</PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN97"
>2.2.3. Print</A
></H2
><P
>The <VAR
CLASS="LITERAL"
>print</VAR
> command provides information such as visibility, type, and value, about a name.
It need not be the name of a variable; functions, namespaces, etc. may also be printed.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN101"
></A
><PRE
CLASS="SCREEN"
>$ nickle
&#62; int x = 2;
&#62; print x
global int x = 2;
&#62; print String
public namespace String {
    public int length (string) &#60;builtin&#62;
    public string new (poly) &#60;builtin&#62;
    public int index (string, string) &#60;builtin&#62;
    public string substr (string, int, int) &#60;builtin&#62;
    public int rindex (string target, string pattern);
    public string dirname (string name);
}
&#62; void function hello() { printf("hello, world\n"); }
&#62; print hello
void hello ()
{
    printf ("hello, world\n");
}
&#62; </PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN103"
>2.2.4. Undefine</A
></H2
><P
>A defined name can be undefined, e.g.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN106"
></A
><PRE
CLASS="SCREEN"
>$ nickle
&#62; print x
No symbol "x" in namespace
&#62; int x = 0;
&#62; print x
global int x = 0;
&#62; undefine x
&#62; print x
No symbol "x" in namespace
&#62;</PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN108"
>2.2.5. Loading files</A
></H2
><P
>The <VAR
CLASS="LITERAL"
>load</VAR
> and <VAR
CLASS="LITERAL"
>library</VAR
> commands evaluate a file at runtime like the <VAR
CLASS="LITERAL"
>-f</VAR
> and <VAR
CLASS="LITERAL"
>-l</VAR
> flags, respectively.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN115"
>2.2.6. Edit</A
></H2
><P
>The <VAR
CLASS="LITERAL"
>edit</VAR
> command invokes <VAR
CLASS="LITERAL"
>$EDITOR</VAR
> on the name given as an argument.
This is particularly useful to change a function while in interactive mode.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN120"
></A
><PRE
CLASS="SCREEN"
>$ nickle
&#62; void function hello() { printf("hello, world\n"); }
&#62; edit hello
49
3
    printf ("hello, world\n");
c
printf ("goodbye, cruel world\n");
wq
53
&#62; print hello
void hello ()
{
    printf ("goodbye, cruel world\n");
}
&#62; </PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN122"
>2.2.7. History</A
></H2
><P
>The <VAR
CLASS="LITERAL"
>history</VAR
> command retrieves the values of the last ten expressions.
With an argument, it instead retrieves the values of that many preceeding values.
With two arguments, it retrieves the specified range in history.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN126"
></A
><PRE
CLASS="SCREEN"
>$ nickle
...
&#62; history
$176    20
$177    5
$178    0
$179    12
$180    12
$181    -2
$182    2
$183    2
$184    0
$185    10
$186    32
&#62; history 3
$184    0
$185    10
$186    32
&#62; history 180,185
$180    12
$181    -2
$182    2
$183    2
$184    0
$185    10</PRE
><P
></P
></DIV
></P
><P
>These history items may be named and used directly:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN129"
></A
><PRE
CLASS="SCREEN"
>&#62; $180 ** 2
144
&#62;</PRE
><P
></P
></DIV
></P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="c32.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="c131.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Nickle Basics</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c32.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Language introduction</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
--- NEW FILE: x818.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Nickle Functions</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Nickle Tutorial"
HREF="index.html"><LINK
REL="UP"
TITLE="Language introduction"
HREF="c131.html"><LINK
REL="PREVIOUS"
TITLE="Control Statements in Nickle"
HREF="x655.html"><LINK
REL="NEXT"
TITLE="Builtins"
HREF="c840.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Nickle Tutorial</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x655.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 3. Language introduction</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="c840.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN818"
>3.4. Nickle Functions</A
></H1
><P
>An example function might be declared like this:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN821"
></A
><PRE
CLASS="SCREEN"
>int gcf ( int a, int b ) {
	int f = 1;
	for ( int i = 2; i &#60;= abs ( a ) &#38;&#38; i &#60;= abs ( b ); ++i )
		while ( ( a // f ) % i == 0 &#38;&#38; ( b // f ) % i == 0 )
			f *= i;
	return f;
}</PRE
><P
></P
></DIV
></P
><P
>First comes the return type of the function, then the function name, then the list of arguments (with types), and finally the statement list in curly braces.
If any types are left off, Nickle assumes <VAR
CLASS="LITERAL"
>poly</VAR
>.
In any case, all typechecking is done at runtime.</P
><P
>A function can take any number of arguments.
The final argument may be succeeded by an ellipses (...) to indicate an arbitrary, variable number of succeeding arguments, each of the type of the final argument; the last argument takes on a list value to store them.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN826"
></A
><PRE
CLASS="SCREEN"
>...
&#62; print sum
int sum (int a, int b ...)
{
    for (int i = 0; i &#60; dim (b); ++i)
	a += b[i];
    return a;
}
&#62; sum(1,2)
3
&#62; sum(4)
4
&#62; sum(1,2,4,6)
13</PRE
><P
></P
></DIV
></P
><P
>Functions are called as in C, with their names followed by argument values in parentheses:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN829"
></A
><PRE
CLASS="SCREEN"
>foo ( "hello", 7.2 );</PRE
><P
></P
></DIV
></P
><P
>Since they are first class, functions can be assigned:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN832"
></A
><PRE
CLASS="SCREEN"
>int(int,int) a = gcf;</PRE
><P
></P
></DIV
></P
><P
>See the section on Copy semantics for details on what functions may be assigned to each other.</P
><P
>Functions may also be declared and used anonymously:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN836"
></A
><PRE
CLASS="SCREEN"
>(int func ( int a, int b ) { return a + b; })(2,3);	/* 5 */</PRE
><P
></P
></DIV
></P
><P
>Replacing the function name with the keyword <VAR
CLASS="LITERAL"
>func</VAR
> indicates its anonymity.</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x655.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="c840.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Control Statements in Nickle</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c131.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Builtins</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
--- NEW FILE: x967.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Math</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Nickle Tutorial"
HREF="index.html"><LINK
REL="UP"
TITLE="Builtins"
HREF="c840.html"><LINK
REL="PREVIOUS"
TITLE="Builtins"
HREF="c840.html"><LINK
REL="NEXT"
TITLE="Strings"
HREF="x1012.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Nickle Tutorial</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="c840.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 4. Builtins</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x1012.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN967"
>4.2. Math</A
></H1
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN969"
>4.2.1. Numbers</A
></H2
><P
>The three numeric types in Nickle--int, rational, and real--have a hierarchical relationship.
Specifically, int is a subset of rational, which is a subset of real.
Ints and rationals are stored internally in infinite precision, and printed as precisely as possible (rationals with repeating portions are represented with curly braces to allow more precision in printing; see the section on Expressions for a discussion of rational constants).
Reals are stored in finite, floating-point representations.
The mantissa defaults to 256 bits long, but this number can be changed.</P
><P
>Whenever performing calculations, Nickle will keep numbers in their most specific format.
For example, the result of '4/2' is an int, because although the result (2) is a rational, it is also an int, and int is more specific.
Similarly, reals are not always in imprecise floating representation; if they are known exactly, they will be represented as rationals or ints.
Nickle will only produce imprecise reals when it has to, as in square roots and logarithms.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN973"
>4.2.2. Operators</A
></H2
><P
>In order to do the Right Thing for a desk calculator, Nickle provides several operators that are not present in C; these are extremely useful.
To force division to produce an integer, even if the result would be a rational, use the '//' integer divide operator, which always rounds its results to ints.
Nickle also has an exponentiation operator '**', which behaves correctly for all exponents, including negative and fractional.
Therefore, sqrt(x) is the same as x**.5, and 1/x is the same as x**-1.
Finally, it provides a factorial operator '!'.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN976"
>4.2.3. The Math namespace</A
></H2
><P
>Nickle provides the builtin namespace Math for useful functions such as trigonometric functions, logarithms, as well as useful constants such as <VAR
CLASS="LITERAL"
>pi</VAR
> and <VAR
CLASS="LITERAL"
>e</VAR
>.</P
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN981"
>4.2.3.1. Logarithms</A
></H3
><PRE
CLASS="SYNOPSIS"
>real log ( real a )</PRE
><PRE
CLASS="SYNOPSIS"
>real log10 ( real a )</PRE
><PRE
CLASS="SYNOPSIS"
>real log2 ( real a )</PRE
><P
>The logarithm of <VAR
CLASS="LITERAL"
>a</VAR
> in base e, ten, and two, respectively.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN988"
></A
><PRE
CLASS="SCREEN"
>$ nickle
&#62; log ( Math::e )
1.000000000000000
&#62; log10 ( 16 ) / log10 ( 4 )	/* change of base formula, log_4 16 */
1.999999999999999
&#62; log2 ( 16 )
3.999999999999999
&#62; </PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN990"
>4.2.3.2. Trigonometric functions</A
></H3
><PRE
CLASS="SYNOPSIS"
>real sin ( real a )</PRE
><PRE
CLASS="SYNOPSIS"
>real cos ( real a )</PRE
><PRE
CLASS="SYNOPSIS"
>real tan ( real a )</PRE
><PRE
CLASS="SYNOPSIS"
>real asin ( real a )</PRE
><PRE
CLASS="SYNOPSIS"
>real acos ( real a )</PRE
><PRE
CLASS="SYNOPSIS"
>real atan ( real a )</PRE
><P
>The sine, cosine, and tangent of <VAR
CLASS="LITERAL"
>a</VAR
>, and the inverse functions.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN1000"
></A
><PRE
CLASS="SCREEN"
>$ nickle
&#62; sin ( pi ) ** 2 + cos ( pi ) **2
1
&#62; atan ( 1 ) * 4
3.141592653589793
&#62; </PRE
><P
></P
></DIV
></P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN1002"
>4.2.3.3. Constants</A
></H3
><PRE
CLASS="SYNOPSIS"
>protected real e</PRE
><PRE
CLASS="SYNOPSIS"
>real pi</PRE
><P
><VAR
CLASS="LITERAL"
>pi</VAR
> and <VAR
CLASS="LITERAL"
>e</VAR
> define the usual constants (3.14..., 2.72...).
<VAR
CLASS="LITERAL"
>e</VAR
> is protected and must be called <VAR
CLASS="LITERAL"
>Math::e</VAR
> to allow ordinary use of the name <VAR
CLASS="LITERAL"
>e</VAR
>.</P
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="c840.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x1012.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Builtins</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c840.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Strings</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
--- c1147.html DELETED ---

--- c13.html DELETED ---

--- c133.html DELETED ---

--- c34.html DELETED ---

--- c863.html DELETED ---

--- index.html DELETED ---

--- x1015.html DELETED ---

--- x1074.html DELETED ---

--- x1205.html DELETED ---

--- x1295.html DELETED ---

--- x1392.html DELETED ---

--- x1544.html DELETED ---

--- x370.html DELETED ---

--- x657.html DELETED ---

--- x80.html DELETED ---

--- x841.html DELETED ---




More information about the Commit mailing list