1
0

index.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <!doctype html>
  2. <meta charset=utf-8>
  3. <title>CodeMirror: OCaml mode</title>
  4. <link rel=stylesheet href=../../lib/codemirror.css>
  5. <link rel=stylesheet href=../../doc/docs.css>
  6. <style type=text/css>
  7. .CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}
  8. </style>
  9. <script src=../../lib/codemirror.js></script>
  10. <script src=../../addon/edit/matchbrackets.js></script>
  11. <script src=ocaml.js></script>
  12. <h1>CodeMirror: OCaml mode</h1>
  13. <textarea id=code>
  14. (* Summing a list of integers *)
  15. let rec sum xs =
  16. match xs with
  17. | [] -&gt; 0
  18. | x :: xs' -&gt; x + sum xs'
  19. (* Quicksort *)
  20. let rec qsort = function
  21. | [] -&gt; []
  22. | pivot :: rest -&gt;
  23. let is_less x = x &lt; pivot in
  24. let left, right = List.partition is_less rest in
  25. qsort left @ [pivot] @ qsort right
  26. (* Fibonacci Sequence *)
  27. let rec fib_aux n a b =
  28. match n with
  29. | 0 -&gt; a
  30. | _ -&gt; fib_aux (n - 1) (a + b) a
  31. let fib n = fib_aux n 0 1
  32. (* Birthday paradox *)
  33. let year_size = 365.
  34. let rec birthday_paradox prob people =
  35. let prob' = (year_size -. float people) /. year_size *. prob in
  36. if prob' &lt; 0.5 then
  37. Printf.printf "answer = %d\n" (people+1)
  38. else
  39. birthday_paradox prob' (people+1) ;;
  40. birthday_paradox 1.0 1
  41. (* Church numerals *)
  42. let zero f x = x
  43. let succ n f x = f (n f x)
  44. let one = succ zero
  45. let two = succ (succ zero)
  46. let add n1 n2 f x = n1 f (n2 f x)
  47. let to_string n = n (fun k -&gt; "S" ^ k) "0"
  48. let _ = to_string (add (succ two) two)
  49. (* Elementary functions *)
  50. let square x = x * x;;
  51. let rec fact x =
  52. if x &lt;= 1 then 1 else x * fact (x - 1);;
  53. (* Automatic memory management *)
  54. let l = 1 :: 2 :: 3 :: [];;
  55. [1; 2; 3];;
  56. 5 :: l;;
  57. (* Polymorphism: sorting lists *)
  58. let rec sort = function
  59. | [] -&gt; []
  60. | x :: l -&gt; insert x (sort l)
  61. and insert elem = function
  62. | [] -&gt; [elem]
  63. | x :: l -&gt;
  64. if elem &lt; x then elem :: x :: l else x :: insert elem l;;
  65. (* Imperative features *)
  66. let add_polynom p1 p2 =
  67. let n1 = Array.length p1
  68. and n2 = Array.length p2 in
  69. let result = Array.create (max n1 n2) 0 in
  70. for i = 0 to n1 - 1 do result.(i) &lt;- p1.(i) done;
  71. for i = 0 to n2 - 1 do result.(i) &lt;- result.(i) + p2.(i) done;
  72. result;;
  73. add_polynom [| 1; 2 |] [| 1; 2; 3 |];;
  74. (* We may redefine fact using a reference cell and a for loop *)
  75. let fact n =
  76. let result = ref 1 in
  77. for i = 2 to n do
  78. result := i * !result
  79. done;
  80. !result;;
  81. fact 5;;
  82. (* Triangle (graphics) *)
  83. let () =
  84. ignore( Glut.init Sys.argv );
  85. Glut.initDisplayMode ~double_buffer:true ();
  86. ignore (Glut.createWindow ~title:"OpenGL Demo");
  87. let angle t = 10. *. t *. t in
  88. let render () =
  89. GlClear.clear [ `color ];
  90. GlMat.load_identity ();
  91. GlMat.rotate ~angle: (angle (Sys.time ())) ~z:1. ();
  92. GlDraw.begins `triangles;
  93. List.iter GlDraw.vertex2 [-1., -1.; 0., 1.; 1., -1.];
  94. GlDraw.ends ();
  95. Glut.swapBuffers () in
  96. GlMat.mode `modelview;
  97. Glut.displayFunc ~cb:render;
  98. Glut.idleFunc ~cb:(Some Glut.postRedisplay);
  99. Glut.mainLoop ()
  100. (* A Hundred Lines of Caml - http://caml.inria.fr/about/taste.en.html *)
  101. (* OCaml page on Wikipedia - http://en.wikipedia.org/wiki/OCaml *)
  102. </textarea>
  103. <script>
  104. var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
  105. mode: 'ocaml',
  106. lineNumbers: true,
  107. matchBrackets: true
  108. });
  109. </script>
  110. <p><strong>MIME types defined:</strong> <code>text/x-ocaml</code>.</p>