From 791c123ba55d726df3da03115a56657b02a02e6f Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Thu, 11 Sep 2014 21:22:02 +0700 Subject: Add a section about strings and characters to the OCaml tutorial. --- ocaml.html.markdown | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) 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 ***) -- cgit v1.2.3