Theme Inspinia
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.

index.html 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></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/codemirror">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. </textarea>
  116. <h2>F# mode</h2>
  117. <textarea id="fsharpCode">
  118. module CodeMirror.FSharp
  119. let rec fib = function
  120. | 0 -> 0
  121. | 1 -> 1
  122. | n -> fib (n - 1) + fib (n - 2)
  123. type Point =
  124. {
  125. x : int
  126. y : int
  127. }
  128. type Color =
  129. | Red
  130. | Green
  131. | Blue
  132. [0 .. 10]
  133. |> List.map ((+) 2)
  134. |> List.fold (fun x y -> x + y) 0
  135. |> printf "%i"
  136. </textarea>
  137. <script>
  138. var ocamlEditor = CodeMirror.fromTextArea(document.getElementById('ocamlCode'), {
  139. mode: 'text/x-ocaml',
  140. lineNumbers: true,
  141. matchBrackets: true
  142. });
  143. var fsharpEditor = CodeMirror.fromTextArea(document.getElementById('fsharpCode'), {
  144. mode: 'text/x-fsharp',
  145. lineNumbers: true,
  146. matchBrackets: true
  147. });
  148. </script>
  149. <p><strong>MIME types defined:</strong> <code>text/x-ocaml</code> (OCaml) and <code>text/x-fsharp</code> (F#).</p>
  150. </article>