OCaml mode
x
1
(* Summing a list of integers *)
2
let rec sum xs =
3
match xs with
4
| [] -> 0
5
| x :: xs' -> x + sum xs'
6
7
(* Quicksort *)
8
let rec qsort = function
9
| [] -> []
10
| pivot :: rest ->
11
let is_less x = x < pivot in
12
let left, right = List.partition is_less rest in
13
qsort left @ [pivot] @ qsort right
14
15
(* Fibonacci Sequence *)
16
let rec fib_aux n a b =
17
match n with
18
| 0 -> a
19
| _ -> fib_aux (n - 1) (a + b) a
20
let fib n = fib_aux n 0 1
21
22
(* Birthday paradox *)
23
let year_size = 365.
24
25
let rec birthday_paradox prob people =
26
let prob' = (year_size -. float people) /. year_size *. prob in
27
if prob' < 0.5 then
28
Printf.printf "answer = %d\n" (people+1)
29
else
30
birthday_paradox prob' (people+1) ;;
31
32
birthday_paradox 1.0 1
33
34
(* Church numerals *)
35
let zero f x = x
36
let succ n f x = f (n f x)
37
let one = succ zero
38
let two = succ (succ zero)
39
let add n1 n2 f x = n1 f (n2 f x)
40
let to_string n = n (fun k -> "S" ^ k) "0"
41
let _ = to_string (add (succ two) two)
42
43
(* Elementary functions *)
44
let square x = x * x;;
45
let rec fact x =
46
if x <= 1 then 1 else x * fact (x - 1);;
47
48
(* Automatic memory management *)
49
let l = 1 :: 2 :: 3 :: [];;
50
[1; 2; 3];;
51
5 :: l;;
52
53
(* Polymorphism: sorting lists *)
54
let rec sort = function
55
| [] -> []
56
| x :: l -> insert x (sort l)
57
58
and insert elem = function
59
| [] -> [elem]
60
| x :: l ->
61
if elem < x then elem :: x :: l else x :: insert elem l;;
62
63
(* Imperative features *)
64
let add_polynom p1 p2 =
65
let n1 = Array.length p1
66
and n2 = Array.length p2 in
67
let result = Array.create (max n1 n2) 0 in
68
for i = 0 to n1 - 1 do result.(i) <- p1.(i) done;
69
for i = 0 to n2 - 1 do result.(i) <- result.(i) + p2.(i) done;
70
result;;
71
add_polynom [| 1; 2 |] [| 1; 2; 3 |];;
72
73
(* We may redefine fact using a reference cell and a for loop *)
74
let fact n =
75
let result = ref 1 in
76
for i = 2 to n do
77
result := i * !result
78
done;
79
!result;;
80
fact 5;;
81
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
101
(* A Hundred Lines of Caml - http://caml.inria.fr/about/taste.en.html *)
102
(* OCaml page on Wikipedia - http://en.wikipedia.org/wiki/OCaml *)
103
F# mode
23
1
module CodeMirror.FSharp
2
3
let rec fib = function
4
| 0 -> 0
5
| 1 -> 1
6
| n -> fib (n - 1) + fib (n - 2)
7
8
type Point =
9
{
10
x : int
11
y : int
12
}
13
14
type Color =
15
| Red
16
| Green
17
| Blue
18
19
[0 .. 10]
20
|> List.map ((+) 2)
21
|> List.fold (fun x y -> x + y) 0
22
|> printf "%i"
23
MIME types defined: text/x-ocaml
(OCaml) and text/x-fsharp
(F#).