scripts/CodeMirror/mode/mllike/index.html (view raw)
1<!doctype html>
2
3<title>CodeMirror: ML-like mode</title>
4<meta charset="utf-8"/>
5<link rel=stylesheet href="../../doc/docs.css">
6
7<link rel=stylesheet href=../../lib/codemirror.css>
8<script src=../../lib/codemirror.js></script>
9<script src=../../addon/edit/matchbrackets.js></script>
10<script src=mllike.js></script>
11<style type=text/css>
12 .CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}
13</style>
14<div id=nav>
15 <a href="https://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png" alt=""></a>
16
17 <ul>
18 <li><a href="../../index.html">Home</a>
19 <li><a href="../../doc/manual.html">Manual</a>
20 <li><a href="https://github.com/codemirror/codemirror">Code</a>
21 </ul>
22 <ul>
23 <li><a href="../index.html">Language modes</a>
24 <li><a class=active href="#">ML-like</a>
25 </ul>
26</div>
27
28<article>
29<h2>OCaml mode</h2>
30
31
32<textarea id="ocamlCode">
33(* Summing a list of integers *)
34let rec sum xs =
35 match xs with
36 | [] -> 0
37 | x :: xs' -> x + sum xs'
38
39(* Quicksort *)
40let rec qsort = function
41 | [] -> []
42 | pivot :: rest ->
43 let is_less x = x < pivot in
44 let left, right = List.partition is_less rest in
45 qsort left @ [pivot] @ qsort right
46
47(* Fibonacci Sequence *)
48let rec fib_aux n a b =
49 match n with
50 | 0 -> a
51 | _ -> fib_aux (n - 1) (a + b) a
52let fib n = fib_aux n 0 1
53
54(* Birthday paradox *)
55let year_size = 365.
56
57let rec birthday_paradox prob people =
58 let prob' = (year_size -. float people) /. year_size *. prob in
59 if prob' < 0.5 then
60 Printf.printf "answer = %d\n" (people+1)
61 else
62 birthday_paradox prob' (people+1) ;;
63
64birthday_paradox 1.0 1
65
66(* Church numerals *)
67let zero f x = x
68let succ n f x = f (n f x)
69let one = succ zero
70let two = succ (succ zero)
71let add n1 n2 f x = n1 f (n2 f x)
72let to_string n = n (fun k -> "S" ^ k) "0"
73let _ = to_string (add (succ two) two)
74
75(* Elementary functions *)
76let square x = x * x;;
77let rec fact x =
78 if x <= 1 then 1 else x * fact (x - 1);;
79
80(* Automatic memory management *)
81let l = 1 :: 2 :: 3 :: [];;
82[1; 2; 3];;
835 :: l;;
84
85(* Polymorphism: sorting lists *)
86let rec sort = function
87 | [] -> []
88 | x :: l -> insert x (sort l)
89
90and insert elem = function
91 | [] -> [elem]
92 | x :: l ->
93 if elem < x then elem :: x :: l else x :: insert elem l;;
94
95(* Imperative features *)
96let add_polynom p1 p2 =
97 let n1 = Array.length p1
98 and n2 = Array.length p2 in
99 let result = Array.create (max n1 n2) 0 in
100 for i = 0 to n1 - 1 do result.(i) <- p1.(i) done;
101 for i = 0 to n2 - 1 do result.(i) <- result.(i) + p2.(i) done;
102 result;;
103add_polynom [| 1; 2 |] [| 1; 2; 3 |];;
104
105(* We may redefine fact using a reference cell and a for loop *)
106let fact n =
107 let result = ref 1 in
108 for i = 2 to n do
109 result := i * !result
110 done;
111 !result;;
112fact 5;;
113
114(* Triangle (graphics) *)
115let () =
116 ignore( Glut.init Sys.argv );
117 Glut.initDisplayMode ~double_buffer:true ();
118 ignore (Glut.createWindow ~title:"OpenGL Demo");
119 let angle t = 10. *. t *. t in
120 let render () =
121 GlClear.clear [ `color ];
122 GlMat.load_identity ();
123 GlMat.rotate ~angle: (angle (Sys.time ())) ~z:1. ();
124 GlDraw.begins `triangles;
125 List.iter GlDraw.vertex2 [-1., -1.; 0., 1.; 1., -1.];
126 GlDraw.ends ();
127 Glut.swapBuffers () in
128 GlMat.mode `modelview;
129 Glut.displayFunc ~cb:render;
130 Glut.idleFunc ~cb:(Some Glut.postRedisplay);
131 Glut.mainLoop ()
132
133(* A Hundred Lines of Caml - http://caml.inria.fr/about/taste.en.html *)
134(* OCaml page on Wikipedia - http://en.wikipedia.org/wiki/OCaml *)
135
136module type S = sig type t end
137
138let x = {|
139 this is a long string
140 with many lines and stuff
141 |}
142
143let b = 0b00110
144let h = 0x123abcd
145let e = 1e-10
146let i = 1.
147let x = 30_000
148let o = 0o1234
149
150[1; 2; 3] (* lists *)
151
1521 @ 2
1531. +. 2.
154</textarea>
155
156<h2>F# mode</h2>
157<textarea id="fsharpCode">
158module CodeMirror.FSharp
159
160let rec fib = function
161 | 0 -> 0
162 | 1 -> 1
163 | n -> fib (n - 1) + fib (n - 2)
164
165type Point =
166 {
167 x : int
168 y : int
169 }
170
171type Color =
172 | Red
173 | Green
174 | Blue
175
176[0 .. 10]
177|> List.map ((+) 2)
178|> List.fold (fun x y -> x + y) 0
179|> printf "%i"
180</textarea>
181
182
183<script>
184 var ocamlEditor = CodeMirror.fromTextArea(document.getElementById('ocamlCode'), {
185 mode: 'text/x-ocaml',
186 lineNumbers: true,
187 matchBrackets: true
188 });
189
190 var fsharpEditor = CodeMirror.fromTextArea(document.getElementById('fsharpCode'), {
191 mode: 'text/x-fsharp',
192 lineNumbers: true,
193 matchBrackets: true
194 });
195</script>
196
197<p><strong>MIME types defined:</strong> <code>text/x-ocaml</code> (OCaml) and <code>text/x-fsharp</code> (F#).</p>
198</article>