openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

index.html (4436B)


      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="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></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/codemirror">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 </textarea>
    136 
    137 <h2>F# mode</h2>
    138 <textarea id="fsharpCode">
    139 module CodeMirror.FSharp
    140 
    141 let rec fib = function
    142     | 0 -> 0
    143     | 1 -> 1
    144     | n -> fib (n - 1) + fib (n - 2)
    145 
    146 type Point =
    147     {
    148         x : int
    149         y : int
    150     }
    151 
    152 type Color =
    153     | Red
    154     | Green
    155     | Blue
    156 
    157 [0 .. 10]
    158 |> List.map ((+) 2)
    159 |> List.fold (fun x y -> x + y) 0
    160 |> printf "%i"
    161 </textarea>
    162 
    163 
    164 <script>
    165   var ocamlEditor = CodeMirror.fromTextArea(document.getElementById('ocamlCode'), {
    166     mode: 'text/x-ocaml',
    167     lineNumbers: true,
    168     matchBrackets: true
    169   });
    170 
    171   var fsharpEditor = CodeMirror.fromTextArea(document.getElementById('fsharpCode'), {
    172     mode: 'text/x-fsharp',
    173     lineNumbers: true,
    174     matchBrackets: true
    175   });
    176 </script>
    177 
    178 <p><strong>MIME types defined:</strong> <code>text/x-ocaml</code> (OCaml) and <code>text/x-fsharp</code> (F#).</p>
    179 </article>