summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNami-Doc <vendethiel@hotmail.fr>2014-09-11 17:22:09 +0200
committerNami-Doc <vendethiel@hotmail.fr>2014-09-11 17:22:09 +0200
commit61735345c4dfa463768f2814ae2c100ed01636af (patch)
tree473c106a0b2ce72e0e7df725a2cd7e0130d7a9d3
parenta7c89acfcf2eb1cd5e172b8f3d5fabccabcd2003 (diff)
parent15fd51c9984fcadcfa83658ae0f01e4b3402c07f (diff)
Merge pull request #759 from dmbaturin/master
Strings/character section and minor fixes in the OCaml tutorial.
-rw-r--r--ocaml.html.markdown45
1 files changed, 43 insertions, 2 deletions
diff --git a/ocaml.html.markdown b/ocaml.html.markdown
index c104823a..7f4e0a9d 100644
--- a/ocaml.html.markdown
+++ b/ocaml.html.markdown
@@ -10,7 +10,7 @@ features.
Along with StandardML and its dialects it belongs to ML language family.
Just like StandardML, there are both a compiler and an interpreter
for OCaml. The interpreter binary is normally called "ocaml" and
-the compiler is "ocamlc.opt". There is also a bytecode compiler, "ocamlc",
+the compiler is "ocamlopt". There is also a bytecode compiler, "ocamlc",
but there are few reasons to use it.
It is strongly and statically typed, but instead of using manually written
@@ -59,11 +59,19 @@ written in curried form.
(* Expressions can be separated by a double semicolon symbol, ";;".
In many cases it's redundant, but in this tutorial we use it after
- every expression for easy pasting into the interpreter shell. *)
+ every expression for easy pasting into the interpreter shell.
+ Unnecessary use of expression separators in source code files
+ is often considered to be a bad style. *)
(* Variable and function declarations use "let" keyword. *)
let x = 10 ;;
+(* OCaml allows single quote characters in identifiers.
+ Single quote doesn't have a special meaning in this case, it's often used
+ in cases when in other languages one would use names like "foo_tmp". *)
+let foo = 1 ;;
+let foo' = foo * 2 ;;
+
(* Since OCaml compiler infers types automatically, you normally don't need to
specify argument types explicitly. However, you can do it if you want or need to. *)
let inc_int (x: int) = x + 1 ;;
@@ -196,6 +204,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 ***)