comparison .cms/lib/codemirror/mode/mllike/index.html @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
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/5"><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/codemirror5">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 *)
34 let rec sum xs =
35 match xs with
36 | [] -&gt; 0
37 | x :: xs' -&gt; x + sum xs'
38
39 (* Quicksort *)
40 let rec qsort = function
41 | [] -&gt; []
42 | pivot :: rest -&gt;
43 let is_less x = x &lt; pivot in
44 let left, right = List.partition is_less rest in
45 qsort left @ [pivot] @ qsort right
46
47 (* Fibonacci Sequence *)
48 let rec fib_aux n a b =
49 match n with
50 | 0 -&gt; a
51 | _ -&gt; fib_aux (n - 1) (a + b) a
52 let fib n = fib_aux n 0 1
53
54 (* Birthday paradox *)
55 let year_size = 365.
56
57 let rec birthday_paradox prob people =
58 let prob' = (year_size -. float people) /. year_size *. prob in
59 if prob' &lt; 0.5 then
60 Printf.printf "answer = %d\n" (people+1)
61 else
62 birthday_paradox prob' (people+1) ;;
63
64 birthday_paradox 1.0 1
65
66 (* Church numerals *)
67 let zero f x = x
68 let succ n f x = f (n f x)
69 let one = succ zero
70 let two = succ (succ zero)
71 let add n1 n2 f x = n1 f (n2 f x)
72 let to_string n = n (fun k -&gt; "S" ^ k) "0"
73 let _ = to_string (add (succ two) two)
74
75 (* Elementary functions *)
76 let square x = x * x;;
77 let rec fact x =
78 if x &lt;= 1 then 1 else x * fact (x - 1);;
79
80 (* Automatic memory management *)
81 let l = 1 :: 2 :: 3 :: [];;
82 [1; 2; 3];;
83 5 :: l;;
84
85 (* Polymorphism: sorting lists *)
86 let rec sort = function
87 | [] -&gt; []
88 | x :: l -&gt; insert x (sort l)
89
90 and insert elem = function
91 | [] -&gt; [elem]
92 | x :: l -&gt;
93 if elem &lt; x then elem :: x :: l else x :: insert elem l;;
94
95 (* Imperative features *)
96 let 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) &lt;- p1.(i) done;
101 for i = 0 to n2 - 1 do result.(i) &lt;- result.(i) + p2.(i) done;
102 result;;
103 add_polynom [| 1; 2 |] [| 1; 2; 3 |];;
104
105 (* We may redefine fact using a reference cell and a for loop *)
106 let fact n =
107 let result = ref 1 in
108 for i = 2 to n do
109 result := i * !result
110 done;
111 !result;;
112 fact 5;;
113
114 (* Triangle (graphics) *)
115 let () =
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
136 module type S = sig type t end
137
138 let x = {|
139 this is a long string
140 with many lines and stuff
141 |}
142
143 let b = 0b00110
144 let h = 0x123abcd
145 let e = 1e-10
146 let i = 1.
147 let x = 30_000
148 let o = 0o1234
149
150 [1; 2; 3] (* lists *)
151
152 1 @ 2
153 1. +. 2.
154 </textarea>
155
156 <h2>F# mode</h2>
157 <textarea id="fsharpCode">
158 module CodeMirror.FSharp
159
160 let rec fib = function
161 | 0 -> 0
162 | 1 -> 1
163 | n -> fib (n - 1) + fib (n - 2)
164
165 type Point =
166 {
167 x : int
168 y : int
169 }
170
171 type 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>