type 'a tree = Leaf of 'a | Branch of 'a tree * 'a * 'a tree
deriving (Show)
type point = { x : float; y : float }
deriving (Show)
let points = Branch (Leaf {x=0.0;
y=0.0;},
{x=2.0; y=2.0},
Branch (Leaf {x=1.0; y=1.0},
{x=1.0; y=0.0},
Leaf {x=0.0; y=1.0}))
Show.show<point tree> points
=>
"Branch
(Leaf {x =0.; y =0.}, {x =2.; y =2.},
Branch
(Leaf {x =1.; y =1.}, {x =1.; y =0.}, Leaf {x =0.; y =1.}))" (* normal *)
let fib = function 0 | 1 -> 1 | n -> fib (n-1) + fib (n-2)
(* memoized *)
let fib = memo 0 | 1 -> 1 | n -> fib (n-1) + fib (n-2)
Automatic generation of type json mytype = Foo | Bar of int * int
(* just add "json" to the type declaration to create to
create the json_of_mytype and mytype_of_json functions *)
* serialization with S-expressions (http://www.janestcapital.com/ocaml/) TABLE user users
COLUMN id SERIAL AUTO PRIMARY KEY
COLUMN name VARCHAR(64) UNIQUE
COLUMN age INT NULLABLE INDEXED
COLUMN password VARCHAR(64)
END
TABLE comment comments
COLUMN id SERIAL AUTO PRIMARY KEY
COLUMN title TEXT
COLUMN text TEXT
COLUMN created_at TIMESTAMPZ
COLUMN author SERIAL FOREIGN(users, id)
END
let minors x = SELECT [User_age < (Some 18)] x
let pauls = SELECT [User_name LIKE "%Paul%"] users
let young_pauls = minors pauls
You can read more about this extension at http://eigenclass.org/hiki/typed-relational-algebra-in-OCaml
For convenience, all the modules are also available under the Batteries namespace. In this case, however, all of Batteries' code will be included in the executable --- that's how OCaml's linker works at the moment.
For instance, if you only use BatList in your code (or do module List = BatList to refer to it with that name), only that module will be included in the binary; if you do
and then refer to the module simply as List, all the other modules in the Batteries hierarchy will also be included in the binary.