on 3345909 help sign in
83 lines 1.5 kB history raw
1let s = "hello world"
3let n : int = 87
5let id x = x
7let add x y = x + y
9let add' x y =
10 let left = x in
11 let right = y in
12 x + y
14let add'' : int -> int -> int = fun x -> fun y -> x + y
16let unwrap_option default opt =
17 match opt with
18 | None -> default
19 | Some v -> v
21let string_of_bool = function true -> "true" | false -> "false"
23let is_a c =
24 if c = 'a' then true
25 else false
27let _ = Printf.printf "%s" "hello"
28let () = Printf.printf "%s\n" "world"
30let x = ref 0
31let _ = x := 1
33type my_bool = True | False
35type shape = Circle of float | Square of float | Rectangle of (float * float)
37type user = {
38 login : string;
39 password : string;
42type 'a my_ref = { mutable ref_value : 'a }
44let (:=) r v = r.ref_value <- v
46let (+) 2 2 = 5
48exception Bad_value of string
50let bad_value_error () = raise (Bad_value "your value is bad and you should feel bad")
52let () =
53 try bad_value_error ()
54 with Bad_value _ -> ()
56let () =
57 try bad_value_error ()
58 with
59 | Bad_value _ -> ()
60 | Not_found -> ()
62module type FOO = sig
63 val foo : 'a -> 'a
64end
66module Foo : FOO = struct
67 let foo x = x
68end
70let greeter = object
71 val greeting = "Hello"
72 method greet name = Printf.sprintf "%s, %s!" greeting name
73end
75let greeting = greeter#greet "world"
77class greeter_factory greeting_text = object (self)
78 val greeting = greeting_text
79 method greet name = Printf.sprintf "%s, %s!" greeting name
80 initializer Printf.printf "Objects will greet the user with \"%s\"\n" greeting
81end
83let g = new greeter_factory "Hi"