<title>CodeMirror: ML-like mode</title>
<link rel=stylesheet href="../../doc/docs.css">
<link rel=stylesheet href=../../lib/codemirror.css>
<script src=../../lib/codemirror.js></script>
<script src=../../addon/edit/matchbrackets.js></script>
<script src=mllike.js></script>
.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}
<a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a>
<li><a href="../../index.html">Home</a>
<li><a href="../../doc/manual.html">Manual</a>
<li><a href="https://github.com/codemirror/codemirror">Code</a>
<li><a href="../index.html">Language modes</a>
<li><a class=active href="#">ML-like</a>
<textarea id="ocamlCode">
(* Summing a list of integers *)
| x :: xs' -> x + sum xs'
let is_less x = x < pivot in
let left, right = List.partition is_less rest in
qsort left @ [pivot] @ qsort right
| _ -> fib_aux (n - 1) (a + b) a
let fib n = fib_aux n 0 1
let rec birthday_paradox prob people =
let prob' = (year_size -. float people) /. year_size *. prob in
Printf.printf "answer = %d\n" (people+1)
birthday_paradox prob' (people+1) ;;
let succ n f x = f (n f x)
let two = succ (succ zero)
let add n1 n2 f x = n1 f (n2 f x)
let to_string n = n (fun k -> "S" ^ k) "0"
let _ = to_string (add (succ two) two)
(* Elementary functions *)
if x <= 1 then 1 else x * fact (x - 1);;
(* Automatic memory management *)
let l = 1 :: 2 :: 3 :: [];;
(* Polymorphism: sorting lists *)
| x :: l -> insert x (sort l)
and insert elem = function
if elem < x then elem :: x :: l else x :: insert elem l;;
(* Imperative features *)
and n2 = Array.length p2 in
let result = Array.create (max n1 n2) 0 in
for i = 0 to n1 - 1 do result.(i) <- p1.(i) done;
for i = 0 to n2 - 1 do result.(i) <- result.(i) + p2.(i) done;
add_polynom [| 1; 2 |] [| 1; 2; 3 |];;
(* We may redefine fact using a reference cell and a for loop *)
(* Triangle (graphics) *)
ignore( Glut.init Sys.argv );
Glut.initDisplayMode ~double_buffer:true ();
ignore (Glut.createWindow ~title:"OpenGL Demo");
let angle t = 10. *. t *. t in
GlClear.clear [ `color ];
GlMat.rotate ~angle: (angle (Sys.time ())) ~z:1. ();
GlDraw.begins `triangles;
List.iter GlDraw.vertex2 [-1., -1.; 0., 1.; 1., -1.];
Glut.displayFunc ~cb:render;
Glut.idleFunc ~cb:(Some Glut.postRedisplay);
(* A Hundred Lines of Caml - http://caml.inria.fr/about/taste.en.html *)
(* OCaml page on Wikipedia - http://en.wikipedia.org/wiki/OCaml *)
<textarea id="fsharpCode">
| n -> fib (n - 1) + fib (n - 2)
|> List.fold (fun x y -> x + y) 0
var ocamlEditor = CodeMirror.fromTextArea(document.getElementById('ocamlCode'), {
var fsharpEditor = CodeMirror.fromTextArea(document.getElementById('fsharpCode'), {
<p><strong>MIME types defined:</strong> <code>text/x-ocaml</code> (OCaml) and <code>text/x-fsharp</code> (F#).</p>