presently 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/bin/presently +13 -0
- data/lib/presently/application.rb +104 -0
- data/lib/presently/clock.rb +77 -0
- data/lib/presently/display_view.rb +73 -0
- data/lib/presently/environment/application.rb +62 -0
- data/lib/presently/page.rb +38 -0
- data/lib/presently/page.xrb +31 -0
- data/lib/presently/presentation.rb +72 -0
- data/lib/presently/presentation_controller.rb +181 -0
- data/lib/presently/presenter_view.rb +264 -0
- data/lib/presently/slide.rb +148 -0
- data/lib/presently/slide_view.rb +92 -0
- data/lib/presently/state.rb +66 -0
- data/lib/presently/version.rb +9 -0
- data/lib/presently.rb +9 -0
- data/license.md +21 -0
- data/public/_components/@socketry/syntax/Syntax/CodeElement.js +337 -0
- data/public/_components/@socketry/syntax/Syntax/Errors.js +52 -0
- data/public/_components/@socketry/syntax/Syntax/Language/apache.js +49 -0
- data/public/_components/@socketry/syntax/Syntax/Language/applescript.js +157 -0
- data/public/_components/@socketry/syntax/Syntax/Language/assembly.js +42 -0
- data/public/_components/@socketry/syntax/Syntax/Language/bash-script.js +108 -0
- data/public/_components/@socketry/syntax/Syntax/Language/bash.js +32 -0
- data/public/_components/@socketry/syntax/Syntax/Language/basic.js +232 -0
- data/public/_components/@socketry/syntax/Syntax/Language/c++.js +1 -0
- data/public/_components/@socketry/syntax/Syntax/Language/c.js +1 -0
- data/public/_components/@socketry/syntax/Syntax/Language/clang.js +201 -0
- data/public/_components/@socketry/syntax/Syntax/Language/cpp.js +1 -0
- data/public/_components/@socketry/syntax/Syntax/Language/csharp.js +166 -0
- data/public/_components/@socketry/syntax/Syntax/Language/css.js +244 -0
- data/public/_components/@socketry/syntax/Syntax/Language/diff.js +24 -0
- data/public/_components/@socketry/syntax/Syntax/Language/go.js +135 -0
- data/public/_components/@socketry/syntax/Syntax/Language/haskell.js +110 -0
- data/public/_components/@socketry/syntax/Syntax/Language/html.js +69 -0
- data/public/_components/@socketry/syntax/Syntax/Language/io.js +68 -0
- data/public/_components/@socketry/syntax/Syntax/Language/java.js +134 -0
- data/public/_components/@socketry/syntax/Syntax/Language/javascript.js +89 -0
- data/public/_components/@socketry/syntax/Syntax/Language/json.js +36 -0
- data/public/_components/@socketry/syntax/Syntax/Language/lisp.js +38 -0
- data/public/_components/@socketry/syntax/Syntax/Language/lua.js +87 -0
- data/public/_components/@socketry/syntax/Syntax/Language/markdown.js +112 -0
- data/public/_components/@socketry/syntax/Syntax/Language/nginx.js +37 -0
- data/public/_components/@socketry/syntax/Syntax/Language/objective-c.js +1 -0
- data/public/_components/@socketry/syntax/Syntax/Language/ocaml.js +225 -0
- data/public/_components/@socketry/syntax/Syntax/Language/pascal.js +166 -0
- data/public/_components/@socketry/syntax/Syntax/Language/patch.js +2 -0
- data/public/_components/@socketry/syntax/Syntax/Language/perl5.js +317 -0
- data/public/_components/@socketry/syntax/Syntax/Language/php-script.js +112 -0
- data/public/_components/@socketry/syntax/Syntax/Language/php.js +18 -0
- data/public/_components/@socketry/syntax/Syntax/Language/plain.js +20 -0
- data/public/_components/@socketry/syntax/Syntax/Language/protobuf.js +77 -0
- data/public/_components/@socketry/syntax/Syntax/Language/python.js +208 -0
- data/public/_components/@socketry/syntax/Syntax/Language/ruby.js +124 -0
- data/public/_components/@socketry/syntax/Syntax/Language/scala.js +81 -0
- data/public/_components/@socketry/syntax/Syntax/Language/smalltalk.js +30 -0
- data/public/_components/@socketry/syntax/Syntax/Language/sql.js +865 -0
- data/public/_components/@socketry/syntax/Syntax/Language/super-collider.js +70 -0
- data/public/_components/@socketry/syntax/Syntax/Language/swift.js +176 -0
- data/public/_components/@socketry/syntax/Syntax/Language/xml.js +76 -0
- data/public/_components/@socketry/syntax/Syntax/Language/xrb.js +33 -0
- data/public/_components/@socketry/syntax/Syntax/Language/yaml.js +29 -0
- data/public/_components/@socketry/syntax/Syntax/Language.js +276 -0
- data/public/_components/@socketry/syntax/Syntax/Loader.js +78 -0
- data/public/_components/@socketry/syntax/Syntax/Match.js +546 -0
- data/public/_components/@socketry/syntax/Syntax/Rule.js +306 -0
- data/public/_components/@socketry/syntax/Syntax.js +356 -0
- data/public/_components/@socketry/syntax/bin/syntax-ast.js +42 -0
- data/public/_components/@socketry/syntax/examples/_template.html +53 -0
- data/public/_components/@socketry/syntax/examples/apache.html +72 -0
- data/public/_components/@socketry/syntax/examples/applescript.html +72 -0
- data/public/_components/@socketry/syntax/examples/assembly.html +74 -0
- data/public/_components/@socketry/syntax/examples/bash.html +90 -0
- data/public/_components/@socketry/syntax/examples/basic.html +87 -0
- data/public/_components/@socketry/syntax/examples/c.html +141 -0
- data/public/_components/@socketry/syntax/examples/clang.html +202 -0
- data/public/_components/@socketry/syntax/examples/csharp.html +110 -0
- data/public/_components/@socketry/syntax/examples/css-colors.html +179 -0
- data/public/_components/@socketry/syntax/examples/custom-theme.html +155 -0
- data/public/_components/@socketry/syntax/examples/diff.html +142 -0
- data/public/_components/@socketry/syntax/examples/examples.css +216 -0
- data/public/_components/@socketry/syntax/examples/go.html +413 -0
- data/public/_components/@socketry/syntax/examples/haskell.html +373 -0
- data/public/_components/@socketry/syntax/examples/html.html +316 -0
- data/public/_components/@socketry/syntax/examples/index.html +97 -0
- data/public/_components/@socketry/syntax/examples/io.html +552 -0
- data/public/_components/@socketry/syntax/examples/java.html +786 -0
- data/public/_components/@socketry/syntax/examples/javascript.html +199 -0
- data/public/_components/@socketry/syntax/examples/json.html +150 -0
- data/public/_components/@socketry/syntax/examples/lisp.html +476 -0
- data/public/_components/@socketry/syntax/examples/lua.html +737 -0
- data/public/_components/@socketry/syntax/examples/markdown.html +121 -0
- data/public/_components/@socketry/syntax/examples/mixed.html +306 -0
- data/public/_components/@socketry/syntax/examples/nginx.html +554 -0
- data/public/_components/@socketry/syntax/examples/ocaml.html +596 -0
- data/public/_components/@socketry/syntax/examples/pascal.html +762 -0
- data/public/_components/@socketry/syntax/examples/perl5.html +488 -0
- data/public/_components/@socketry/syntax/examples/php-script.html +142 -0
- data/public/_components/@socketry/syntax/examples/php.html +95 -0
- data/public/_components/@socketry/syntax/examples/plain.html +222 -0
- data/public/_components/@socketry/syntax/examples/protobuf.html +405 -0
- data/public/_components/@socketry/syntax/examples/python.html +82 -0
- data/public/_components/@socketry/syntax/examples/readme.md +79 -0
- data/public/_components/@socketry/syntax/examples/ruby.html +58 -0
- data/public/_components/@socketry/syntax/examples/scala.html +41 -0
- data/public/_components/@socketry/syntax/examples/smalltalk.html +436 -0
- data/public/_components/@socketry/syntax/examples/sql.html +373 -0
- data/public/_components/@socketry/syntax/examples/super-collider.html +55 -0
- data/public/_components/@socketry/syntax/examples/swift.html +176 -0
- data/public/_components/@socketry/syntax/examples/wrap-demo.html +103 -0
- data/public/_components/@socketry/syntax/examples/xml.html +112 -0
- data/public/_components/@socketry/syntax/examples/xrb.html +37 -0
- data/public/_components/@socketry/syntax/examples/yaml.html +72 -0
- data/public/_components/@socketry/syntax/license.md +21 -0
- data/public/_components/@socketry/syntax/package-lock.json +834 -0
- data/public/_components/@socketry/syntax/package.json +43 -0
- data/public/_components/@socketry/syntax/readme.md +162 -0
- data/public/_components/@socketry/syntax/test/Syntax/CodeElement.js +306 -0
- data/public/_components/@socketry/syntax/test/Syntax/ErrorHandling.js +85 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/apache.js +153 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/applescript.js +198 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/assembly.js +209 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/bash-script.js +225 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/bash.js +162 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/basic.js +265 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/clang.js +390 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/csharp.js +436 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/css.js +431 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/diff.js +206 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/go.js +386 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/haskell.js +454 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/html.js +111 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/io.js +229 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/java.js +362 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/javascript.js +101 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/json.js +101 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/lisp.js +224 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/lua.js +307 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/markdown.js +163 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/nginx.js +267 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/ocaml.js +299 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/pascal.js +311 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/perl5.js +333 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/php-script.js +197 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/php.js +92 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/plain.js +327 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/protobuf.js +294 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/python.js +213 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/ruby.js +70 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/scala.js +75 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/smalltalk.js +223 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/sql.js +281 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/super-collider.js +66 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/swift.js +71 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/xml.js +170 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/xrb.js +57 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/yaml.js +123 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language.js +62 -0
- data/public/_components/@socketry/syntax/test/Syntax/Match.js +40 -0
- data/public/_components/@socketry/syntax/test/Syntax/Rule.js +251 -0
- data/public/_components/@socketry/syntax/test/Syntax.js +38 -0
- data/public/_components/@socketry/syntax/test/helpers/ast-matcher.js +90 -0
- data/public/_components/@socketry/syntax/themes/base/apache.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/applescript.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/assembly.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/bash.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/basic.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/c.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/clang.css +0 -0
- data/public/_components/@socketry/syntax/themes/base/csharp.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/css.css +22 -0
- data/public/_components/@socketry/syntax/themes/base/diff.css +48 -0
- data/public/_components/@socketry/syntax/themes/base/go.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/haskell.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/html.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/io.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/java.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/javascript.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/json.css +41 -0
- data/public/_components/@socketry/syntax/themes/base/lisp.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/lua.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/markdown.css +16 -0
- data/public/_components/@socketry/syntax/themes/base/nginx.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/ocaml.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/pascal.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/perl5.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/php-script.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/php.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/plain.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/protobuf.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/python.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/ruby.css +23 -0
- data/public/_components/@socketry/syntax/themes/base/scala.css +3 -0
- data/public/_components/@socketry/syntax/themes/base/smalltalk.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/sql.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/super-collider.css +33 -0
- data/public/_components/@socketry/syntax/themes/base/swift.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/syntax.css +63 -0
- data/public/_components/@socketry/syntax/themes/base/xml.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/xrb.css +29 -0
- data/public/_components/@socketry/syntax/themes/base/yaml.css +1 -0
- data/public/_components/@socketry/syntax/themes/theming.md +233 -0
- data/public/_components/@socketry/syntax/update-examples.js +135 -0
- data/public/_static/index.css +593 -0
- data/public/application.js +147 -0
- data/readme.md +69 -0
- data/releases.md +3 -0
- data/templates/code.xrb +12 -0
- data/templates/default.xrb +5 -0
- data/templates/image.xrb +8 -0
- data/templates/section.xrb +5 -0
- data/templates/title.xrb +8 -0
- data/templates/translation.xrb +8 -0
- data/templates/two_column.xrb +8 -0
- metadata +280 -0
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>OCaml Examples - @socketry/syntax</title>
|
|
7
|
+
<link rel="stylesheet" href="examples.css">
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<header>
|
|
11
|
+
<h1>OCaml Examples</h1>
|
|
12
|
+
<p class="subtitle">OCaml syntax highlighting</p>
|
|
13
|
+
</header>
|
|
14
|
+
|
|
15
|
+
<nav>
|
|
16
|
+
<a href="index.html">Back to Examples</a>
|
|
17
|
+
</nav>
|
|
18
|
+
|
|
19
|
+
<h1>OCaml Code Example</h1>
|
|
20
|
+
|
|
21
|
+
<p>OCaml is a powerful functional programming language with strong static typing, pattern matching, and algebraic data types.</p>
|
|
22
|
+
|
|
23
|
+
<div class="example">
|
|
24
|
+
<h2>Basic Syntax and Variables</h2>
|
|
25
|
+
<p class="description">Example:</p>
|
|
26
|
+
|
|
27
|
+
<pre><code class="language-ocaml">(* Single variable binding *)
|
|
28
|
+
let x = 42
|
|
29
|
+
|
|
30
|
+
(* Multiple bindings *)
|
|
31
|
+
let y = 3.14
|
|
32
|
+
let name = "Alice"
|
|
33
|
+
let flag = true
|
|
34
|
+
|
|
35
|
+
(* Type annotations (optional) *)
|
|
36
|
+
let count : int = 10
|
|
37
|
+
let message : string = "Hello"
|
|
38
|
+
|
|
39
|
+
(* Immutable by default - rebind with let *)
|
|
40
|
+
let x = x + 1
|
|
41
|
+
|
|
42
|
+
(* Mutable references *)
|
|
43
|
+
let counter = ref 0
|
|
44
|
+
let () = counter := !counter + 1
|
|
45
|
+
</code></pre>
|
|
46
|
+
|
|
47
|
+
<div class="example">
|
|
48
|
+
<h2>Functions</h2>
|
|
49
|
+
<p class="description">Example:</p>
|
|
50
|
+
|
|
51
|
+
<pre><code class="language-ocaml">(* Simple function *)
|
|
52
|
+
let square x = x * x
|
|
53
|
+
|
|
54
|
+
(* Multiple parameters *)
|
|
55
|
+
let add x y = x + y
|
|
56
|
+
|
|
57
|
+
(* Type annotations *)
|
|
58
|
+
let multiply (x : int) (y : int) : int = x * y
|
|
59
|
+
|
|
60
|
+
(* Anonymous functions (lambdas) *)
|
|
61
|
+
let increment = fun x -> x + 1
|
|
62
|
+
|
|
63
|
+
(* Alternative lambda syntax *)
|
|
64
|
+
let decrement = function x -> x - 1
|
|
65
|
+
|
|
66
|
+
(* Recursive functions *)
|
|
67
|
+
let rec factorial n =
|
|
68
|
+
if n <= 1 then 1
|
|
69
|
+
else n * factorial (n - 1)
|
|
70
|
+
|
|
71
|
+
(* Mutually recursive functions *)
|
|
72
|
+
let rec is_even n =
|
|
73
|
+
if n = 0 then true
|
|
74
|
+
else is_odd (n - 1)
|
|
75
|
+
and is_odd n =
|
|
76
|
+
if n = 0 then false
|
|
77
|
+
else is_even (n - 1)
|
|
78
|
+
|
|
79
|
+
(* Higher-order functions *)
|
|
80
|
+
let apply_twice f x = f (f x)
|
|
81
|
+
|
|
82
|
+
let result = apply_twice square 2 (* 16 *)
|
|
83
|
+
</code></pre>
|
|
84
|
+
|
|
85
|
+
<div class="example">
|
|
86
|
+
<h2>Pattern Matching</h2>
|
|
87
|
+
<p class="description">Example:</p>
|
|
88
|
+
|
|
89
|
+
<pre><code class="language-ocaml">(* Basic pattern matching *)
|
|
90
|
+
let describe_number n =
|
|
91
|
+
match n with
|
|
92
|
+
| 0 -> "zero"
|
|
93
|
+
| 1 -> "one"
|
|
94
|
+
| 2 -> "two"
|
|
95
|
+
| _ -> "many"
|
|
96
|
+
|
|
97
|
+
(* Pattern matching with guards *)
|
|
98
|
+
let classify_number n =
|
|
99
|
+
match n with
|
|
100
|
+
| n when n < 0 -> "negative"
|
|
101
|
+
| 0 -> "zero"
|
|
102
|
+
| n when n > 0 && n < 10 -> "small positive"
|
|
103
|
+
| _ -> "large positive"
|
|
104
|
+
|
|
105
|
+
(* Matching tuples *)
|
|
106
|
+
let describe_point (x, y) =
|
|
107
|
+
match (x, y) with
|
|
108
|
+
| (0, 0) -> "origin"
|
|
109
|
+
| (x, 0) -> "on x-axis"
|
|
110
|
+
| (0, y) -> "on y-axis"
|
|
111
|
+
| (x, y) -> "general point"
|
|
112
|
+
|
|
113
|
+
(* Function syntax with pattern matching *)
|
|
114
|
+
let fib = function
|
|
115
|
+
| 0 -> 0
|
|
116
|
+
| 1 -> 1
|
|
117
|
+
| n -> fib (n - 1) + fib (n - 2)
|
|
118
|
+
</code></pre>
|
|
119
|
+
|
|
120
|
+
<div class="example">
|
|
121
|
+
<h2>Lists</h2>
|
|
122
|
+
<p class="description">Example:</p>
|
|
123
|
+
|
|
124
|
+
<pre><code class="language-ocaml">(* List literals *)
|
|
125
|
+
let empty = []
|
|
126
|
+
let numbers = [1; 2; 3; 4; 5]
|
|
127
|
+
let names = ["Alice"; "Bob"; "Charlie"]
|
|
128
|
+
|
|
129
|
+
(* Cons operator :: *)
|
|
130
|
+
let extended = 0 :: numbers (* [0; 1; 2; 3; 4; 5] *)
|
|
131
|
+
|
|
132
|
+
(* List concatenation *)
|
|
133
|
+
let combined = [1; 2] @ [3; 4] (* [1; 2; 3; 4] *)
|
|
134
|
+
|
|
135
|
+
(* Pattern matching on lists *)
|
|
136
|
+
let rec sum_list lst =
|
|
137
|
+
match lst with
|
|
138
|
+
| [] -> 0
|
|
139
|
+
| hd :: tl -> hd + sum_list tl
|
|
140
|
+
|
|
141
|
+
(* Head and tail *)
|
|
142
|
+
let first_element = function
|
|
143
|
+
| [] -> None
|
|
144
|
+
| hd :: _ -> Some hd
|
|
145
|
+
|
|
146
|
+
(* List.map *)
|
|
147
|
+
let doubled = List.map (fun x -> x * 2) [1; 2; 3]
|
|
148
|
+
|
|
149
|
+
(* List.filter *)
|
|
150
|
+
let evens = List.filter (fun x -> x mod 2 = 0) [1; 2; 3; 4; 5; 6]
|
|
151
|
+
|
|
152
|
+
(* List.fold_left *)
|
|
153
|
+
let sum = List.fold_left (+) 0 [1; 2; 3; 4; 5]
|
|
154
|
+
|
|
155
|
+
(* List.fold_right *)
|
|
156
|
+
let product = List.fold_right ( * ) [1; 2; 3; 4; 5] 1
|
|
157
|
+
</code></pre>
|
|
158
|
+
|
|
159
|
+
<div class="example">
|
|
160
|
+
<h2>Tuples and Records</h2>
|
|
161
|
+
<p class="description">Example:</p>
|
|
162
|
+
|
|
163
|
+
<pre><code class="language-ocaml">(* Tuples *)
|
|
164
|
+
let pair = (42, "answer")
|
|
165
|
+
let triple = (1, 2.0, "three")
|
|
166
|
+
|
|
167
|
+
(* Destructuring tuples *)
|
|
168
|
+
let (x, y) = pair
|
|
169
|
+
|
|
170
|
+
(* Tuple functions *)
|
|
171
|
+
let first (x, _) = x
|
|
172
|
+
let second (_, y) = y
|
|
173
|
+
|
|
174
|
+
(* Records *)
|
|
175
|
+
type person = {
|
|
176
|
+
name : string;
|
|
177
|
+
age : int;
|
|
178
|
+
email : string;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
let alice = {
|
|
182
|
+
name = "Alice";
|
|
183
|
+
age = 30;
|
|
184
|
+
email = "alice@example.com";
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
(* Accessing record fields *)
|
|
188
|
+
let name = alice.name
|
|
189
|
+
let age = alice.age
|
|
190
|
+
|
|
191
|
+
(* Record update (creates new record) *)
|
|
192
|
+
let older_alice = { alice with age = 31 }
|
|
193
|
+
|
|
194
|
+
(* Pattern matching on records *)
|
|
195
|
+
let greet person =
|
|
196
|
+
match person with
|
|
197
|
+
| { name; age; _ } ->
|
|
198
|
+
Printf.sprintf "Hello, %s! You are %d years old." name age
|
|
199
|
+
</code></pre>
|
|
200
|
+
|
|
201
|
+
<div class="example">
|
|
202
|
+
<h2>Algebraic Data Types (Variants)</h2>
|
|
203
|
+
<p class="description">Example:</p>
|
|
204
|
+
|
|
205
|
+
<pre><code class="language-ocaml">(* Simple enumeration *)
|
|
206
|
+
type color = Red | Green | Blue
|
|
207
|
+
|
|
208
|
+
let color_to_string = function
|
|
209
|
+
| Red -> "red"
|
|
210
|
+
| Green -> "green"
|
|
211
|
+
| Blue -> "blue"
|
|
212
|
+
|
|
213
|
+
(* Variants with data *)
|
|
214
|
+
type shape =
|
|
215
|
+
| Circle of float
|
|
216
|
+
| Rectangle of float * float
|
|
217
|
+
| Triangle of float * float * float
|
|
218
|
+
|
|
219
|
+
let area = function
|
|
220
|
+
| Circle r -> 3.14159 *. r *. r
|
|
221
|
+
| Rectangle (w, h) -> w *. h
|
|
222
|
+
| Triangle (a, b, c) ->
|
|
223
|
+
let s = (a +. b +. c) /. 2.0 in
|
|
224
|
+
sqrt (s *. (s -. a) *. (s -. b) *. (s -. c))
|
|
225
|
+
|
|
226
|
+
(* Option type (built-in variant) *)
|
|
227
|
+
let safe_divide x y =
|
|
228
|
+
if y = 0 then None
|
|
229
|
+
else Some (x / y)
|
|
230
|
+
|
|
231
|
+
let result = match safe_divide 10 2 with
|
|
232
|
+
| Some v -> Printf.sprintf "Result: %d" v
|
|
233
|
+
| None -> "Division by zero"
|
|
234
|
+
|
|
235
|
+
(* Result type for error handling *)
|
|
236
|
+
type ('a, 'e) result =
|
|
237
|
+
| Ok of 'a
|
|
238
|
+
| Error of 'e
|
|
239
|
+
|
|
240
|
+
let parse_int s =
|
|
241
|
+
try Ok (int_of_string s)
|
|
242
|
+
with Failure _ -> Error "Invalid integer"
|
|
243
|
+
</code></pre>
|
|
244
|
+
|
|
245
|
+
<div class="example">
|
|
246
|
+
<h2>Modules</h2>
|
|
247
|
+
<p class="description">Example:</p>
|
|
248
|
+
|
|
249
|
+
<pre><code class="language-ocaml">(* Module definition *)
|
|
250
|
+
module Stack = struct
|
|
251
|
+
type 'a t = 'a list
|
|
252
|
+
|
|
253
|
+
let empty = []
|
|
254
|
+
|
|
255
|
+
let push x s = x :: s
|
|
256
|
+
|
|
257
|
+
let pop = function
|
|
258
|
+
| [] -> None
|
|
259
|
+
| x :: xs -> Some (x, xs)
|
|
260
|
+
|
|
261
|
+
let peek = function
|
|
262
|
+
| [] -> None
|
|
263
|
+
| x :: _ -> Some x
|
|
264
|
+
|
|
265
|
+
let is_empty s = (s = [])
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
(* Using the module *)
|
|
269
|
+
let s = Stack.empty
|
|
270
|
+
let s = Stack.push 1 s
|
|
271
|
+
let s = Stack.push 2 s
|
|
272
|
+
let top = Stack.peek s (* Some 2 *)
|
|
273
|
+
|
|
274
|
+
(* Module type (signature) *)
|
|
275
|
+
module type QUEUE = sig
|
|
276
|
+
type 'a t
|
|
277
|
+
val empty : 'a t
|
|
278
|
+
val enqueue : 'a -> 'a t -> 'a t
|
|
279
|
+
val dequeue : 'a t -> ('a * 'a t) option
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
(* Module implementing a signature *)
|
|
283
|
+
module Queue : QUEUE = struct
|
|
284
|
+
type 'a t = 'a list
|
|
285
|
+
|
|
286
|
+
let empty = []
|
|
287
|
+
|
|
288
|
+
let enqueue x q = q @ [x]
|
|
289
|
+
|
|
290
|
+
let dequeue = function
|
|
291
|
+
| [] -> None
|
|
292
|
+
| x :: xs -> Some (x, xs)
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
(* Functor (parameterized module) *)
|
|
296
|
+
module type COMPARABLE = sig
|
|
297
|
+
type t
|
|
298
|
+
val compare : t -> t -> int
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
module Set (Elem : COMPARABLE) = struct
|
|
302
|
+
type t = Elem.t list
|
|
303
|
+
|
|
304
|
+
let empty = []
|
|
305
|
+
|
|
306
|
+
let rec mem x = function
|
|
307
|
+
| [] -> false
|
|
308
|
+
| y :: ys ->
|
|
309
|
+
match Elem.compare x y with
|
|
310
|
+
| 0 -> true
|
|
311
|
+
| _ -> mem x ys
|
|
312
|
+
|
|
313
|
+
let rec add x s =
|
|
314
|
+
if mem x s then s
|
|
315
|
+
else x :: s
|
|
316
|
+
end
|
|
317
|
+
</code></pre>
|
|
318
|
+
|
|
319
|
+
<div class="example">
|
|
320
|
+
<h2>Polymorphic Functions</h2>
|
|
321
|
+
<p class="description">Example:</p>
|
|
322
|
+
|
|
323
|
+
<pre><code class="language-ocaml">(* Identity function *)
|
|
324
|
+
let id x = x
|
|
325
|
+
|
|
326
|
+
(* Type is inferred as: 'a -> 'a *)
|
|
327
|
+
let n = id 42
|
|
328
|
+
let s = id "hello"
|
|
329
|
+
|
|
330
|
+
(* Composition *)
|
|
331
|
+
let compose f g x = f (g x)
|
|
332
|
+
(* Type: ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c *)
|
|
333
|
+
|
|
334
|
+
(* Pipeline operator *)
|
|
335
|
+
let (|>) x f = f x
|
|
336
|
+
|
|
337
|
+
let result = 5
|
|
338
|
+
|> fun x -> x * 2
|
|
339
|
+
|> fun x -> x + 1
|
|
340
|
+
|> fun x -> x * x
|
|
341
|
+
(* result = 121 *)
|
|
342
|
+
|
|
343
|
+
(* Polymorphic list operations *)
|
|
344
|
+
let rec map f = function
|
|
345
|
+
| [] -> []
|
|
346
|
+
| x :: xs -> f x :: map f xs
|
|
347
|
+
|
|
348
|
+
let rec filter pred = function
|
|
349
|
+
| [] -> []
|
|
350
|
+
| x :: xs ->
|
|
351
|
+
if pred x then x :: filter pred xs
|
|
352
|
+
else filter pred xs
|
|
353
|
+
</code></pre>
|
|
354
|
+
|
|
355
|
+
<div class="example">
|
|
356
|
+
<h2>Imperative Features</h2>
|
|
357
|
+
<p class="description">Example:</p>
|
|
358
|
+
|
|
359
|
+
<pre><code class="language-ocaml">(* References (mutable cells) *)
|
|
360
|
+
let counter = ref 0
|
|
361
|
+
|
|
362
|
+
let increment () =
|
|
363
|
+
counter := !counter + 1
|
|
364
|
+
|
|
365
|
+
let get_count () = !counter
|
|
366
|
+
|
|
367
|
+
(* Arrays (mutable) *)
|
|
368
|
+
let arr = [| 1; 2; 3; 4; 5 |]
|
|
369
|
+
|
|
370
|
+
let first = arr.(0) (* Access *)
|
|
371
|
+
let () = arr.(0) <- 10 (* Update *)
|
|
372
|
+
|
|
373
|
+
(* For loops *)
|
|
374
|
+
for i = 0 to 9 do
|
|
375
|
+
Printf.printf "%d " i
|
|
376
|
+
done
|
|
377
|
+
|
|
378
|
+
(* Reverse for loop *)
|
|
379
|
+
for i = 9 downto 0 do
|
|
380
|
+
Printf.printf "%d " i
|
|
381
|
+
done
|
|
382
|
+
|
|
383
|
+
(* While loops *)
|
|
384
|
+
let i = ref 0
|
|
385
|
+
while !i < 10 do
|
|
386
|
+
Printf.printf "%d " !i;
|
|
387
|
+
i := !i + 1
|
|
388
|
+
done
|
|
389
|
+
|
|
390
|
+
(* Sequences with ; *)
|
|
391
|
+
let run () =
|
|
392
|
+
print_endline "First";
|
|
393
|
+
print_endline "Second";
|
|
394
|
+
print_endline "Third"
|
|
395
|
+
</code></pre>
|
|
396
|
+
|
|
397
|
+
<div class="example">
|
|
398
|
+
<h2>Exception Handling</h2>
|
|
399
|
+
<p class="description">Example:</p>
|
|
400
|
+
|
|
401
|
+
<pre><code class="language-ocaml">(* Define exception *)
|
|
402
|
+
exception Invalid_argument of string
|
|
403
|
+
exception Not_found
|
|
404
|
+
|
|
405
|
+
(* Raise exception *)
|
|
406
|
+
let divide x y =
|
|
407
|
+
if y = 0 then
|
|
408
|
+
raise (Invalid_argument "Division by zero")
|
|
409
|
+
else
|
|
410
|
+
x / y
|
|
411
|
+
|
|
412
|
+
(* Catch exception *)
|
|
413
|
+
let safe_divide x y =
|
|
414
|
+
try
|
|
415
|
+
Some (divide x y)
|
|
416
|
+
with Invalid_argument msg ->
|
|
417
|
+
print_endline msg;
|
|
418
|
+
None
|
|
419
|
+
|
|
420
|
+
(* Multiple exception handlers *)
|
|
421
|
+
let process input =
|
|
422
|
+
try
|
|
423
|
+
let result = complex_operation input in
|
|
424
|
+
Ok result
|
|
425
|
+
with
|
|
426
|
+
| Not_found -> Error "Not found"
|
|
427
|
+
| Invalid_argument msg -> Error msg
|
|
428
|
+
| Failure msg -> Error ("Failure: " ^ msg)
|
|
429
|
+
</code></pre>
|
|
430
|
+
|
|
431
|
+
<div class="example">
|
|
432
|
+
<h2>Object-Oriented Programming</h2>
|
|
433
|
+
<p class="description">Example:</p>
|
|
434
|
+
|
|
435
|
+
<pre><code class="language-ocaml">(* Class definition *)
|
|
436
|
+
class counter init = object (self)
|
|
437
|
+
val mutable count = init
|
|
438
|
+
|
|
439
|
+
method get = count
|
|
440
|
+
|
|
441
|
+
method increment =
|
|
442
|
+
count <- count + 1
|
|
443
|
+
|
|
444
|
+
method reset =
|
|
445
|
+
count <- init
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
(* Creating objects *)
|
|
449
|
+
let c = new counter 0
|
|
450
|
+
let () = c#increment
|
|
451
|
+
let value = c#get (* 1 *)
|
|
452
|
+
|
|
453
|
+
(* Inheritance *)
|
|
454
|
+
class ['a] stack = object (self)
|
|
455
|
+
val mutable items : 'a list = []
|
|
456
|
+
|
|
457
|
+
method push x =
|
|
458
|
+
items <- x :: items
|
|
459
|
+
|
|
460
|
+
method pop =
|
|
461
|
+
match items with
|
|
462
|
+
| [] -> None
|
|
463
|
+
| x :: xs ->
|
|
464
|
+
items <- xs;
|
|
465
|
+
Some x
|
|
466
|
+
|
|
467
|
+
method peek =
|
|
468
|
+
match items with
|
|
469
|
+
| [] -> None
|
|
470
|
+
| x :: _ -> Some x
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
(* Class with inheritance *)
|
|
474
|
+
class counted_stack = object
|
|
475
|
+
inherit [int] stack as super
|
|
476
|
+
|
|
477
|
+
val mutable push_count = 0
|
|
478
|
+
|
|
479
|
+
method push x =
|
|
480
|
+
super#push x;
|
|
481
|
+
push_count <- push_count + 1
|
|
482
|
+
|
|
483
|
+
method get_push_count = push_count
|
|
484
|
+
end
|
|
485
|
+
</code></pre>
|
|
486
|
+
|
|
487
|
+
<div class="example">
|
|
488
|
+
<h2>Advanced Type Features</h2>
|
|
489
|
+
<p class="description">Example:</p>
|
|
490
|
+
|
|
491
|
+
<pre><code class="language-ocaml">(* Parametric polymorphism *)
|
|
492
|
+
type 'a option =
|
|
493
|
+
| Some of 'a
|
|
494
|
+
| None
|
|
495
|
+
|
|
496
|
+
type ('a, 'b) result =
|
|
497
|
+
| Ok of 'a
|
|
498
|
+
| Error of 'b
|
|
499
|
+
|
|
500
|
+
(* Recursive types *)
|
|
501
|
+
type 'a tree =
|
|
502
|
+
| Leaf
|
|
503
|
+
| Node of 'a * 'a tree * 'a tree
|
|
504
|
+
|
|
505
|
+
let rec tree_sum = function
|
|
506
|
+
| Leaf -> 0
|
|
507
|
+
| Node (value, left, right) ->
|
|
508
|
+
value + tree_sum left + tree_sum right
|
|
509
|
+
|
|
510
|
+
(* Phantom types *)
|
|
511
|
+
type 'a validated =
|
|
512
|
+
| Validated of string
|
|
513
|
+
|
|
514
|
+
let validate (s : string) : bool validated =
|
|
515
|
+
Validated s
|
|
516
|
+
|
|
517
|
+
let use_validated (Validated s : bool validated) =
|
|
518
|
+
print_endline s
|
|
519
|
+
|
|
520
|
+
(* GADTs (Generalized Algebraic Data Types) *)
|
|
521
|
+
type _ expr =
|
|
522
|
+
| Int : int -> int expr
|
|
523
|
+
| Bool : bool -> bool expr
|
|
524
|
+
| Add : int expr * int expr -> int expr
|
|
525
|
+
| If : bool expr * 'a expr * 'a expr -> 'a expr
|
|
526
|
+
|
|
527
|
+
let rec eval : type a. a expr -> a = function
|
|
528
|
+
| Int n -> n
|
|
529
|
+
| Bool b -> b
|
|
530
|
+
| Add (e1, e2) -> eval e1 + eval e2
|
|
531
|
+
| If (cond, then_e, else_e) ->
|
|
532
|
+
if eval cond then eval then_e else eval else_e
|
|
533
|
+
</code></pre>
|
|
534
|
+
|
|
535
|
+
<div class="example">
|
|
536
|
+
<h2>Practical Examples</h2>
|
|
537
|
+
<p class="description">Example:</p>
|
|
538
|
+
|
|
539
|
+
<pre><code class="language-ocaml">(* Binary search tree *)
|
|
540
|
+
type 'a bst =
|
|
541
|
+
| Empty
|
|
542
|
+
| Node of 'a * 'a bst * 'a bst
|
|
543
|
+
|
|
544
|
+
let rec insert x = function
|
|
545
|
+
| Empty -> Node (x, Empty, Empty)
|
|
546
|
+
| Node (y, left, right) ->
|
|
547
|
+
if x < y then Node (y, insert x left, right)
|
|
548
|
+
else if x > y then Node (y, left, insert x right)
|
|
549
|
+
else Node (y, left, right)
|
|
550
|
+
|
|
551
|
+
let rec search x = function
|
|
552
|
+
| Empty -> false
|
|
553
|
+
| Node (y, left, right) ->
|
|
554
|
+
if x = y then true
|
|
555
|
+
else if x < y then search x left
|
|
556
|
+
else search x right
|
|
557
|
+
|
|
558
|
+
(* Quicksort *)
|
|
559
|
+
let rec quicksort = function
|
|
560
|
+
| [] -> []
|
|
561
|
+
| pivot :: rest ->
|
|
562
|
+
let less = List.filter (fun x -> x < pivot) rest in
|
|
563
|
+
let greater = List.filter (fun x -> x >= pivot) rest in
|
|
564
|
+
quicksort less @ [pivot] @ quicksort greater
|
|
565
|
+
|
|
566
|
+
(* Memoization *)
|
|
567
|
+
let memoize f =
|
|
568
|
+
let table = Hashtbl.create 100 in
|
|
569
|
+
fun x ->
|
|
570
|
+
try Hashtbl.find table x
|
|
571
|
+
with Not_found ->
|
|
572
|
+
let result = f x in
|
|
573
|
+
Hashtbl.add table x result;
|
|
574
|
+
result
|
|
575
|
+
|
|
576
|
+
let rec fib_slow n =
|
|
577
|
+
if n <= 1 then n
|
|
578
|
+
else fib_slow (n - 1) + fib_slow (n - 2)
|
|
579
|
+
|
|
580
|
+
let fib_fast = memoize fib_slow
|
|
581
|
+
|
|
582
|
+
(* Map implementation *)
|
|
583
|
+
module StringMap = Map.Make(String)
|
|
584
|
+
|
|
585
|
+
let empty_map = StringMap.empty
|
|
586
|
+
let map_with_data =
|
|
587
|
+
StringMap.empty
|
|
588
|
+
|> StringMap.add "one" 1
|
|
589
|
+
|> StringMap.add "two" 2
|
|
590
|
+
|> StringMap.add "three" 3
|
|
591
|
+
|
|
592
|
+
let value = StringMap.find "two" map_with_data (* 2 *)
|
|
593
|
+
</code></pre>
|
|
594
|
+
|
|
595
|
+
</body>
|
|
596
|
+
</html>
|