Friday, June 24, 2011

OCaml notes

Functional core of Objective CAML:

http://caml.inria.fr/pub/docs/oreilly-book/html/book-ora015.html

Variables are variable:
# let p = 10 ;;
val p : int = 10
# let k x = (x, p, x+p) ;;
val k : int -> int * int * int =
# k p ;;
- : int * int * int = 10, 10, 20
# let p = 1000 ;;
val p : int = 1000
# k p ;;
- : int * int * int = 1000, 10, 1010
 
http://caml.inria.fr/pub/docs/manual-ocaml/expr.html

Looping:
while expr1 do  expr2 done
for name =  expr1 to  expr2 do  expr3 done
for name =  expr1 downto  expr2 do  expr3 done 
Exceptions: try ... with ... -> ...
Objects
Coercion (casting to superclass): subobject :> superclass
Message sending (calling method on object): object # method
Object duplication: Oo.copy
Recursive functions: rec
String concatenation: ^
 
Characters delimited by ` not ': `a`
 
http://caml.inria.fr/pub/docs/oreilly-book/html/book-ora016.html#toc21 
Tuples
Records
Sum Types
Constant Constructor: type suit = Spades | Hearts | Diamonds | Clubs ;; 
  Constructor with Args: type card = King of suit | Queen of suit | Knight of suit | Knave of suit | Minor_card of suit * int | Trump of int | Joker ;;
Recursive Types
Parameterised Types: type ('a, 'b) list2 =