some notes on ocaml
facts:
- ML is a general-purpose functional programming language
- Caml is a dialect of the ML
- OCaml is the most popular variant of the Caml language
- Caml Light's successor is OCaml
- OCaml is also known as: Objective Caml
Documentation and user¢s manual
ONE
ebal@mylaptop ~$ ocaml
OCaml version 4.00.1
# 1+2*3;;
– : int = 7
# 1;;
– : int = 1
# 1.1;;
– : float = 1.1
# 'a';;
– : char = 'a'
# "a";;
– : string = "a"
# abc;;
Error: Unbound value abc # "abc";;
– : string = "abc"
# true;;
– : bool = true
# false;;
– : bool = false
# (1<2);;
- : bool = true
# (1>2);;
– : bool = false
# ["hello", "world"];;
– : (string * string) list = [("hello", "world")]
# ["hello"; "world"];;
– : string list = ["hello"; "world"]
# "a" :: ["hello"; "world"];;
– : string list = ["a"; "hello"; "world"]
Two
1. integer * float
# 2 * 3.14 ;;
Error: This expression has type float but an expression was expected of type
int
# 2.0 * 3.14 ;;
Error: This expression has type float but an expression was expected of type
int
# 2,0 * 3,14 ;;
– : int * int * int = (2, 0, 14)
# 2 *. * 3.14 ;;
Error: Syntax error
# 2.0 *. 3.14 ;;
– : float = 6.28
2. Unbound
# 2.0 *. pi ;;
Error: Unbound value pi
(fun is function)
# fun pi -> 2.0 *. pi ;;
– : float -> float = <fun>
(ðåñíÜò ôï 3.14 ùò argument)
# ( fun pi -> 2.0 *. pi ) 3.14 ;;
– : float = 6.28
3. functional
let pi = 3.14;;
let result = fun r -> r *. pi ;;
result 2.0 ;;
result 5.0 ;;