You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
4.6 KiB

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