summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2014-09-11 21:22:02 +0700
committerDaniil Baturin <daniil@baturin.org>2014-09-11 21:22:02 +0700
commit791c123ba55d726df3da03115a56657b02a02e6f (patch)
treee5ac275e7d1fa4996c7141c8fecb119f27e7b185
parent6eea532330ac11ce261dd9b3e765099b1288fb17 (diff)
Add a section about strings and characters to the OCaml tutorial.
-rw-r--r--ocaml.html.markdown33
1 files changed, 33 insertions, 0 deletions
diff --git a/ocaml.html.markdown b/ocaml.html.markdown
index 747a52f5..5c236b1a 100644
--- a/ocaml.html.markdown
+++ b/ocaml.html.markdown
@@ -196,6 +196,39 @@ let my_array = [| 1; 2; 3 |] ;;
my_array.(0) ;;
+(*** Strings and characters ***)
+
+(* Use double quotes for string literals. *)
+let my_str = "Hello world" ;;
+
+(* Use single quotes for character literals. *)
+let my_char = 'a' ;;
+
+(* Single and double quotes are not interchangeable. *)
+let bad_str = 'syntax error' ;; (* Syntax error. *)
+
+(* This will give you a single character string, not a character. *)
+let single_char_str = "w" ;;
+
+(* Strings can be concatenated with the "^" operator. *)
+let some_str = "hello" ^ "world" ;;
+
+(* Strings are not arrays of characters.
+ You can't mix characters and strings in expressions.
+ You can convert a character to a string with "String.make 1 my_char".
+ There are more convenient functions for this purpose in additional
+ libraries such as Core.Std that may not be installed and/or loaded
+ by default. *)
+let ocaml = (String.make 1 'O') ^ "Caml" ;;
+
+(* There is a printf function. *)
+Printf.printf "%d %s" 99 "bottles of beer" ;;
+
+(* Unformatted read and write functions are there too. *)
+print_string "hello world\n" ;;
+print_endline "hello world" ;;
+let line = read_line () ;;
+
(*** User-defined data types ***)