Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
5 lars 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 {
11
        border-top: 1px solid black;
12
        border-bottom: 1px solid black;
13
    }
14
</style>
15
<div id=nav>
16
    <a href="http://codemirror.net">
17
        <h1>CodeMirror</h1>
18
        <img id=logo src="../../doc/logo.png">
19
    </a>
20
    <ul>
21
        <li>
22
            <a href="../../index.html">Home</a>
23
            <li>
24
                <a href="../../doc/manual.html">Manual</a>
25
                <li>
26
                    <a href="https://github.com/codemirror/codemirror">Code</a>
27
    </ul>
28
    <ul>
29
        <li>
30
            <a href="../index.html">Language modes</a>
31
            <li>
32
                <a class=active href="#">ML-like</a>
33
    </ul>
34
</div>
35
<article>
36
    <h2>OCaml mode</h2>
37
    <textarea id="ocamlCode"> (* Summing a list of integers *) let rec sum xs = match xs with | [] -&gt; 0 | x :: xs' -&gt; x + sum xs' (* Quicksort *) let rec qsort = function | [] -&gt; [] | pivot :: rest -&gt; let is_less x = x &lt; pivot in let left, right = List.partition
38
        is_less rest in qsort left @ [pivot] @ qsort right (* Fibonacci Sequence *) let rec fib_aux n a b = match n with | 0 -&gt; a | _ -&gt; fib_aux (n - 1) (a + b) a let fib n = fib_aux n 0 1 (* Birthday paradox *) let year_size = 365. let rec birthday_paradox
39
        prob people = let prob' = (year_size -. float people) /. year_size *. prob in if prob' &lt; 0.5 then Printf.printf "answer = %d\n" (people+1) else birthday_paradox prob' (people+1) ;; birthday_paradox 1.0 1 (* Church numerals *) let zero f x =
40
        x let succ n f x = f (n f x) let one = succ zero let two = succ (succ zero) let add n1 n2 f x = n1 f (n2 f x) let to_string n = n (fun k -&gt; "S" ^ k) "0" let _ = to_string (add (succ two) two) (* Elementary functions *) let square x = x * x;;
41
        let rec fact x = if x &lt;= 1 then 1 else x * fact (x - 1);; (* Automatic memory management *) let l = 1 :: 2 :: 3 :: [];; [1; 2; 3];; 5 :: l;; (* Polymorphism: sorting lists *) let rec sort = function | [] -&gt; [] | x :: l -&gt; insert x (sort
42
        l) and insert elem = function | [] -&gt; [elem] | x :: l -&gt; if elem &lt; x then elem :: x :: l else x :: insert elem l;; (* Imperative features *) let add_polynom p1 p2 = let n1 = Array.length p1 and n2 = Array.length p2 in let result = Array.create
43
        (max n1 n2) 0 in for i = 0 to n1 - 1 do result.(i) &lt;- p1.(i) done; for i = 0 to n2 - 1 do result.(i) &lt;- result.(i) + p2.(i) done; result;; add_polynom [| 1; 2 |] [| 1; 2; 3 |];; (* We may redefine fact using a reference cell and a for loop
44
        *) let fact n = let result = ref 1 in for i = 2 to n do result := i * !result done; !result;; fact 5;; (* Triangle (graphics) *) let () = ignore( Glut.init Sys.argv ); Glut.initDisplayMode ~double_buffer:true (); ignore (Glut.createWindow ~title:"OpenGL
45
        Demo"); let angle t = 10. *. t *. t in let render () = GlClear.clear [ `color ]; GlMat.load_identity (); GlMat.rotate ~angle: (angle (Sys.time ())) ~z:1. (); GlDraw.begins `triangles; List.iter GlDraw.vertex2 [-1., -1.; 0., 1.; 1., -1.]; GlDraw.ends
46
        (); Glut.swapBuffers () in GlMat.mode `modelview; Glut.displayFunc ~cb:render; Glut.idleFunc ~cb:(Some Glut.postRedisplay); Glut.mainLoop () (* A Hundred Lines of Caml - http://caml.inria.fr/about/taste.en.html *) (* OCaml page on Wikipedia -
47
        http://en.wikipedia.org/wiki/OCaml *) </textarea>
48
    <h2>F# mode</h2>
49
    <textarea id="fsharpCode"> module CodeMirror.FSharp let rec fib = function | 0 -> 0 | 1 -> 1 | n -> fib (n - 1) + fib (n - 2) type Point = { x : int y : int } type Color = | Red | Green | Blue [0 .. 10] |> List.map ((+) 2) |> List.fold (fun x y -> x + y) 0 |> printf "%i" </textarea>
50
    <script>
51
        var ocamlEditor = CodeMirror.fromTextArea(document.getElementById('ocamlCode'),
52
        {
53
            mode: 'text/x-ocaml',
54
            lineNumbers: true,
55
            matchBrackets: true
56
        });
57
        var fsharpEditor = CodeMirror.fromTextArea(document.getElementById('fsharpCode'),
58
        {
59
            mode: 'text/x-fsharp',
60
            lineNumbers: true,
61
            matchBrackets: true
62
        });
63
    </script>
64
    <p>
65
        <strong>MIME types defined:</strong> <code>text/x-ocaml</code> (OCaml) and <code>text/x-fsharp</code> (F#).</p>
66
</article>