Teach Time Encyclopedia - Learn About Our World
Home Page
Teach Time
Featured Topics

United States
by state

CITYology

Academic Disciplines

Historical Timelines

Themed Timelines

Calendars

Reference Tables

Biographies

How-tos



Sunday, September 07, 2008

Ocaml

Objective CAML, also known as Ocaml or O'Caml for short, is an advanced programming language based on the ML family. Caml stands for Categorically Abstract Machine Language.

To the functional and imperative features of Standard ML, it adds object-oriented concepts and some minor syntax differences.

Ocaml provides both a bytecode compiler and a native code compiler, and the latter has been ported to a large number of platforms. The code generated by the native compiler is typically comparable to C/C++ in speed.

Powerful features of the language include a static type system, pattern matching, an exception mechanism, and automatic memory management. Ocaml distributions include general purpose libraries and are available for a wide range of platforms, including Unix and Windows.

Ocaml is used in a wide range of applications including thoerem proving and program analysis. It is used as an introductory language in many places, including Caltech.

Example code

(* List manipulation *)

 (* The procedures below work on lists of any type *)

(* Length of a list *) let rec length = function |[] -> 0 |x::xs -> 1 + length xs;;

(* Insertion sort *) (* An order relation must exist *) let rec sort = function |[] -> [] |x::xs -> insert x (sort xs) and insert e = function |[] -> [e] |x::xs -> if x > e then e::x::xs else x::(insert e xs);;

# let l = ["The"; "quick"; "brown"; "fox"; "jumps"; "over"; "the"; "lazy"; "dog"];

# length l;; - : int = 7

# sort l;; - : string list = ["The"; "brown"; "dog"; "fox"; "jumps"; "lazy"; "over"; "quick"; "the"]

(* Trees *)

 (* Definition of a binary tree, of any type 'a *)
 type 'a tree = Node | Tree of ('a tree * 'a * 'a tree) ;;

let a = Tree(Node, 4, Tree(Node, 2, Node)) ;; (* Height of a tree *) let rec height = function |Node -> 0 |Tree(left, _, right) -> 1 + max (height left) (height right) ;;

# height a;; - : int = 2

(* Search of a root using the dichotomy method *)

 let rec dicho f min max eps =
   let fmin = f min and fmax = f max in
     if fmin *. fmax > 0.
     then failwith "No root"
     else if max -. min < eps then (min, max) (* return an interval *)
     else let mid = (min +. max) /. 2. in
       if (f mid) *. fmin < 0.
       then dicho f min mid eps
       else dicho f mid max eps ;;

(* Approximation of the square root of 2 *) # dicho (fun x -> x *. x -. 2.) 0. 10. 0.000000001;; - : float * float = (1.4142135618, 1.41421356238)

See also:
F sharp programming language

External Links



Internet Hotel Solutions

Site Sponsors
AC Units
Baltimore Harbor
Boot Camp Grads
Bra Size
Burkittsville
College Hotels
Digital Harbor
Free Cell Phones
Golden Hare Travel
Golf Vacations
Golf Courses
Gourmet
Hair Styles
Hippodrome
iWoman
Lesson Plans
Maryland Hotels
MD Genealogy
Minor League Stuff
Motel Site
Ocean City
OC Real Estate
Old Agers
Office Supplies
Orlando
Pet Friendly Hotel
Room Prices
Savannah, GA
Ski Vacations
South Baltimore
Student Teaching
Travel Sources
University Hotels
Visit Military Bases
Washington, DC

Brought to you by NoChildLeftBehind.com and the Beaches and Towns Network, LLC.