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