diff options
Diffstat (limited to 'ocaml.html.markdown')
-rw-r--r-- | ocaml.html.markdown | 55 |
1 files changed, 49 insertions, 6 deletions
diff --git a/ocaml.html.markdown b/ocaml.html.markdown index fd7ca36e..7f4e0a9d 100644 --- a/ocaml.html.markdown +++ b/ocaml.html.markdown @@ -1,5 +1,5 @@ --- -language: "OCaml" +language: OCaml contributors: - ["Daniil Baturin", "http://baturin.org/"] --- @@ -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 @@ -20,6 +20,7 @@ source of confusion for beginners. When you are in the top level loop, OCaml will print the inferred type after you enter an expression. + ``` # let inc x = x + 1 ;; val inc : int -> int = <fun> @@ -42,6 +43,7 @@ val inc : int -> int val add : int -> int -> int val a : int ``` + Note that type signatures of functions of multiple arguments are written in curried form. @@ -57,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 ;; @@ -194,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 ***) @@ -304,6 +347,6 @@ sum_int_list t ;; ## Further reading -* Visit the official website to get the compiler and read the docs: http://ocaml.org/ -* Try interactive tutorials and a web-based interpreter by OCaml Pro: http://try.ocamlpro.com/ -* Read "OCaml for the skeptical" course: http://www2.lib.uchicago.edu/keith/ocaml-class/home.html +* Visit the official website to get the compiler and read the docs: <http://ocaml.org/> +* Try interactive tutorials and a web-based interpreter by OCaml Pro: <http://try.ocamlpro.com/> +* Read "OCaml for the skeptical" course: <http://www2.lib.uchicago.edu/keith/ocaml-class/home.html> |