Difference between revisions of "OCaml"

From HaskellWiki
Jump to navigation Jump to search
Line 76: Line 76:
 
== Conceptual differences ==
 
== Conceptual differences ==
   
OCaml's <tt>let</tt> is non-recursive and strict by default, but has keywords <tt>rec</tt> (as in <tt>let rec</tt>) and <tt>lazy</tt> to introduce the Haskell behaviour.
+
OCaml's <tt>let</tt> is non-recursive and strict by default, but has keywords <tt>rec</tt> (as in <tt>let rec</tt>) and <tt>lazy</tt> to introduce more Haskellish behaviour.
   
 
OCaml is ''impure''. Although it makes heavy use of immutable data, it also has mutable references and arrays available, and IO is performed by ordinary functions.
 
OCaml is ''impure''. Although it makes heavy use of immutable data, it also has mutable references and arrays available, and IO is performed by ordinary functions.

Revision as of 23:08, 11 December 2012

OCaml is a functional programming language in the ML family, an extension of the Caml language with object-oriented constructs.

This page aims to cover some of its differences from Haskell.

Syntactic dictionary

Language Haskell OCaml Comments
Anonymous functions
\x y -> ...
fun x y -> ...
Multiple assignments
let
  x = 4
  y = 5
 in ...
let x = 4
and y = 5
 in ...
Types
Int, Bool, (Double, Char), a
int, bool, float * char, 'a
float is a double type
Type signatures
const :: a -> b -> a
const : 'a -> 'b -> 'a
Signatures usually omitted in OCaml
Type declarations
data A = B Int | C Char Bool
x = B 3
y = C 'a' True
type a = B of int | C of char * bool
let x = B 3
and y = C ('a', true)
Parametrised types
data D a = D (a -> a)
data E a b = L a | R b
type 'a d = D of ('a -> 'a)
type ('a, 'b) e = L of 'a | R of 'b
Pattern matching
case x of
  A x -> ...
  C a b -> ...
match x with
  B x -> ...
  C (a, b) -> ...

Conceptual differences

OCaml's let is non-recursive and strict by default, but has keywords rec (as in let rec) and lazy to introduce more Haskellish behaviour.

OCaml is impure. Although it makes heavy use of immutable data, it also has mutable references and arrays available, and IO is performed by ordinary functions.