source: ipython/IPython/frontend/html/notebook/static/codemirror/mode/ocaml/index.html @ 58574f

Revision 58574f, 3.3 KB checked in by Matthias BUSSONNIER <bussonniermatthias@…>, 21 months ago (diff)

update CodeMirror2 to 2.32

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