~ubuntu-branches/ubuntu/karmic/ocaml-doc/karmic

« back to all changes in this revision

Viewing changes to ocaml.html/manual021.html

  • Committer: Bazaar Package Importer
  • Author(s): Vanicat Rémi
  • Date: 2002-02-05 10:51:43 UTC
  • Revision ID: james.westby@ubuntu.com-20020205105143-a061tunf8tev07ne
Tags: 3.04-4
* New debian maintainer
* Split doc-base file
* Move to non-free
* Change the copyright file to the copyright of the documentation
* remove FAQs (their license prohibit their redistribution)
* corrected the examples

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
 
2
            "http://www.w3.org/TR/REC-html40/loose.dtd">
 
3
<HTML>
 
4
<HEAD>
 
5
 
 
6
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 
7
<META name="GENERATOR" content="hevea 1.06-7 of 2001-11-14">
 
8
<TITLE>
 
9
 Language extensions
 
10
</TITLE>
 
11
</HEAD>
 
12
<BODY TEXT=black BGCOLOR=white>
 
13
<A HREF="manual008.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
 
14
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
 
15
<A HREF="manual022.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
 
16
<HR>
 
17
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
 
18
<TR><TD BGCOLOR="#2de52d"><DIV ALIGN=center><TABLE>
 
19
<TR><TD><A NAME="htoc89"><B><FONT SIZE=6>Chapter&nbsp;7</FONT></B></A></TD>
 
20
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=6>Language extensions</FONT></B></TD>
 
21
</TR></TABLE></DIV></TD>
 
22
</TR></TABLE> <A NAME="c:extensions"></A>
 
23
<BR>
 
24
This chapter describes the language features that are implemented in
 
25
Objective Caml, but not described in the Objective Caml
 
26
reference manual. In contrast with the fairly stable kernel language
 
27
that is described in the reference manual, the extensions presented
 
28
here are still experimental, and may be removed or changed in the
 
29
future.<BR>
 
30
<BR>
 
31
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
 
32
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
 
33
<TR><TD><A NAME="htoc90"><B><FONT SIZE=5>7.1</FONT></B></A></TD>
 
34
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Streams and stream parsers</FONT></B></TD>
 
35
</TR></TABLE></DIV></TD>
 
36
</TR></TABLE>
 
37
<A NAME="s:streams"></A><BR>
 
38
Streams and stream parsers are no longer part of the Objective Caml
 
39
language, but available through a CamlP4 syntax extension. See the
 
40
CamlP4 reference manual for more information. Objective Caml programs
 
41
that use streams and stream parsers can be compiled with the
 
42
<TT>-pp camlp4o</TT> option to <TT>ocamlc</TT> and <TT>ocamlopt</TT>.<BR>
 
43
<BR>
 
44
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
 
45
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
 
46
<TR><TD><A NAME="htoc91"><B><FONT SIZE=5>7.2</FONT></B></A></TD>
 
47
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Range patterns</FONT></B></TD>
 
48
</TR></TABLE></DIV></TD>
 
49
</TR></TABLE><BR>
 
50
In patterns, Objective Caml recognizes the form
 
51
<TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>c</FONT></I></TT> <TT><FONT COLOR=blue>'</FONT></TT> <TT><FONT COLOR=blue>..</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>d</FONT></I></TT> <TT><FONT COLOR=blue>'</FONT></TT>
 
52
(two character literals separated by&nbsp;<TT>..</TT>) as shorthand for the pattern
 
53
<DIV ALIGN=center>
 
54
<TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>c</FONT></I></TT> <TT><FONT COLOR=blue>'</FONT></TT> <TT><FONT COLOR=blue>|</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>c</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>'</FONT></TT> <TT><FONT COLOR=blue>|</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>c</FONT></I></TT><SUB><FONT SIZE=2>2</FONT></SUB> <TT><FONT COLOR=blue>'</FONT></TT> <TT><FONT COLOR=blue>|</FONT></TT> ...
 
55
 <TT><FONT COLOR=blue>|</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>c</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB> <TT><FONT COLOR=blue>'</FONT></TT> <TT><FONT COLOR=blue>|</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>d</FONT></I></TT> <TT><FONT COLOR=blue>'</FONT></TT>
 
56
</DIV>
 
57
where <I>c</I><SUB><FONT SIZE=2>1</FONT></SUB>, <I>c</I><SUB><FONT SIZE=2>2</FONT></SUB>, ..., <I>c</I><SUB><FONT SIZE=2><I>n</I></FONT></SUB> are the characters
 
58
that occur between <I>c</I> and <I>d</I> in the ASCII character set. For
 
59
instance, the pattern <TT>'0'..'9'</TT> matches all characters that are digits.<BR>
 
60
<BR>
 
61
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
 
62
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
 
63
<TR><TD><A NAME="htoc92"><B><FONT SIZE=5>7.3</FONT></B></A></TD>
 
64
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Assertion checking</FONT></B></TD>
 
65
</TR></TABLE></DIV></TD>
 
66
</TR></TABLE>
 
67
<A NAME="@manual.kwd168"></A><BR>
 
68
Objective Caml supports the <TT>assert</TT> construct to check debugging assertions.
 
69
The expression <TT><FONT COLOR=blue>assert</FONT></TT> <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> evaluates the expression <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> and
 
70
returns <TT>()</TT> if <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> evaluates to <TT>true</TT>. Otherwise, the exception
 
71
<TT>Assert_failure</TT> is raised with the source file name and the
 
72
location of <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> as arguments. Assertion
 
73
checking can be turned off with the <TT>-noassert</TT> compiler option.<BR>
 
74
<BR>
 
75
As a special case, <TT>assert false</TT> is reduced to
 
76
<TT>raise (Assert_failure ...)</TT>, which is polymorphic (and
 
77
is not turned off by the <TT>-noassert</TT> option).
 
78
<A NAME="@manual3"></A><BR>
 
79
<BR>
 
80
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
 
81
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
 
82
<TR><TD><A NAME="htoc93"><B><FONT SIZE=5>7.4</FONT></B></A></TD>
 
83
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Deferred computations</FONT></B></TD>
 
84
</TR></TABLE></DIV></TD>
 
85
</TR></TABLE>
 
86
<A NAME="@manual.kwd169"></A><BR>
 
87
The expression <TT><FONT COLOR=blue>lazy</FONT></TT> <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> returns a value <I>v</I> of type <TT>Lazy.t</TT> that
 
88
encapsulates the computation of <TT><I><FONT COLOR=maroon>expr</FONT></I></TT>. The argument <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> is not
 
89
evaluated at this point in the program. Instead, its evaluation will
 
90
be performed the first time <TT>Lazy.force</TT> is applied to the value
 
91
<I>v</I>, returning the actual value of <TT><I><FONT COLOR=maroon>expr</FONT></I></TT>. Subsequent applications
 
92
of <TT>Lazy.force</TT> to <I>v</I> do not evaluate <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> again.<BR>
 
93
<BR>
 
94
The expression <TT><FONT COLOR=blue>lazy</FONT></TT> <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> is equivalent to
 
95
<TT><FONT COLOR=blue>ref</FONT></TT>(<TT><FONT COLOR=blue>Lazy.Delayed</FONT></TT>(<TT><FONT COLOR=blue>fun</FONT></TT> <TT><FONT COLOR=blue>()</FONT></TT> <TT><FONT COLOR=blue>-&gt;</FONT></TT> <TT><I><FONT COLOR=maroon>expr</FONT></I></TT>)).
 
96
For more information, see the description of module <TT>Lazy</TT> in the
 
97
standard library (
 
98
<a HREF=libref/Lazy.html>Module <tt>Lazy</tt></a>
 
99
 ).
 
100
<A NAME="@manual4"></A><A NAME="@manual5"></A><BR>
 
101
<BR>
 
102
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
 
103
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
 
104
<TR><TD><A NAME="htoc94"><B><FONT SIZE=5>7.5</FONT></B></A></TD>
 
105
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Local modules</FONT></B></TD>
 
106
</TR></TABLE></DIV></TD>
 
107
</TR></TABLE>
 
108
<A NAME="@manual.kwd170"></A>
 
109
<A NAME="@manual.kwd171"></A><BR>
 
110
The expression
 
111
<TT><FONT COLOR=blue>let</FONT></TT> <TT><FONT COLOR=blue>module</FONT></TT> <TT><I><FONT COLOR=maroon>module-name</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> <TT><FONT COLOR=blue>in</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>expr</FONT></I></TT>
 
112
locally binds the module expression <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> to the identifier
 
113
<TT><I><FONT COLOR=maroon>module-name</FONT></I></TT> during the evaluation of the expression <TT><I><FONT COLOR=maroon>expr</FONT></I></TT>.
 
114
It then returns the value of <TT><I><FONT COLOR=maroon>expr</FONT></I></TT>. For example:
 
115
<PRE>
 
116
        let remove_duplicates comparison_fun string_list =
 
117
          let module StringSet =
 
118
            Set.Make(struct type t = string
 
119
                            let compare = comparison_fun end) in
 
120
          StringSet.elements
 
121
            (List.fold_right StringSet.add string_list StringSet.empty)
 
122
</PRE>
 
123
<HR>
 
124
<A HREF="manual008.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
 
125
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
 
126
<A HREF="manual022.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
 
127
</BODY>
 
128
</HTML>