c10a84fade08619fa363cee6beb9ddbb08af582c
[myslice.git] / third-party / codemirror-3.15 / mode / ocaml / index.html
1 <!doctype html>
2 <meta charset=utf-8>
3 <title>CodeMirror: OCaml mode</title>
4
5 <link rel=stylesheet href=../../lib/codemirror.css>
6 <link rel=stylesheet href=../../doc/docs.css>
7
8 <style type=text/css>
9   .CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}
10 </style>
11
12 <script src=../../lib/codemirror.js></script>
13 <script src=../../addon/edit/matchbrackets.js></script>
14 <script src=ocaml.js></script>
15
16 <h1>CodeMirror: OCaml mode</h1>
17
18 <textarea id=code>
19 (* Summing a list of integers *)
20 let rec sum xs =
21   match xs with
22     | []       -&gt; 0
23     | x :: xs' -&gt; x + sum xs'
24
25 (* Quicksort *)
26 let rec qsort = function
27    | [] -&gt; []
28    | pivot :: rest -&gt;
29        let is_less x = x &lt; pivot in
30        let left, right = List.partition is_less rest in
31        qsort left @ [pivot] @ qsort right
32
33 (* Fibonacci Sequence *)
34 let rec fib_aux n a b =
35   match n with
36   | 0 -&gt; a
37   | _ -&gt; fib_aux (n - 1) (a + b) a
38 let fib n = fib_aux n 0 1
39
40 (* Birthday paradox *)
41 let year_size = 365.
42
43 let rec birthday_paradox prob people =
44     let prob' = (year_size -. float people) /. year_size *. prob  in
45     if prob' &lt; 0.5 then
46         Printf.printf "answer = %d\n" (people+1)
47     else
48         birthday_paradox prob' (people+1) ;;
49
50 birthday_paradox 1.0 1
51
52 (* Church numerals *)
53 let zero f x = x
54 let succ n f x = f (n f x)
55 let one = succ zero
56 let two = succ (succ zero)
57 let add n1 n2 f x = n1 f (n2 f x)
58 let to_string n = n (fun k -&gt; "S" ^ k) "0"
59 let _ = to_string (add (succ two) two)
60
61 (* Elementary functions *)
62 let square x = x * x;;
63 let rec fact x =
64   if x &lt;= 1 then 1 else x * fact (x - 1);;
65
66 (* Automatic memory management *)
67 let l = 1 :: 2 :: 3 :: [];;
68 [1; 2; 3];;
69 5 :: l;;
70
71 (* Polymorphism: sorting lists *)
72 let rec sort = function
73   | [] -&gt; []
74   | x :: l -&gt; insert x (sort l)
75
76 and insert elem = function
77   | [] -&gt; [elem]
78   | x :: l -&gt; 
79       if elem &lt; x then elem :: x :: l else x :: insert elem l;;
80
81 (* Imperative features *)
82 let add_polynom p1 p2 =
83   let n1 = Array.length p1
84   and n2 = Array.length p2 in
85   let result = Array.create (max n1 n2) 0 in
86   for i = 0 to n1 - 1 do result.(i) &lt;- p1.(i) done;
87   for i = 0 to n2 - 1 do result.(i) &lt;- result.(i) + p2.(i) done;
88   result;;
89 add_polynom [| 1; 2 |] [| 1; 2; 3 |];;
90
91 (* We may redefine fact using a reference cell and a for loop *)
92 let fact n =
93   let result = ref 1 in
94   for i = 2 to n do
95     result := i * !result
96    done;
97    !result;;
98 fact 5;;
99
100 (* Triangle (graphics) *)
101 let () =
102   ignore( Glut.init Sys.argv );
103   Glut.initDisplayMode ~double_buffer:true ();
104   ignore (Glut.createWindow ~title:"OpenGL Demo");
105   let angle t = 10. *. t *. t in
106   let render () =
107     GlClear.clear [ `color ];
108     GlMat.load_identity ();
109     GlMat.rotate ~angle: (angle (Sys.time ())) ~z:1. ();
110     GlDraw.begins `triangles;
111     List.iter GlDraw.vertex2 [-1., -1.; 0., 1.; 1., -1.];
112     GlDraw.ends ();
113     Glut.swapBuffers () in
114   GlMat.mode `modelview;
115   Glut.displayFunc ~cb:render;
116   Glut.idleFunc ~cb:(Some Glut.postRedisplay);
117   Glut.mainLoop ()
118
119 (* A Hundred Lines of Caml - http://caml.inria.fr/about/taste.en.html *)
120 (* OCaml page on Wikipedia - http://en.wikipedia.org/wiki/OCaml *)
121 </textarea>
122
123 <script>
124   var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
125     mode: 'ocaml',
126     lineNumbers: true,
127     matchBrackets: true
128   });
129 </script>
130
131 <p><strong>MIME types defined:</strong> <code>text/x-ocaml</code>.</p>