OCaml mode
(* A Hundred Lines of Caml - http://caml.inria.fr/about/taste.en.html *)
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
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#).