Difference between revisions of "OCaml"

From HaskellWiki
Jump to navigation Jump to search
(New page: '''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 ...)
 
(clarifications on comments)
Tag: visualeditor-switched
 
(17 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
This page aims to cover some of its differences from Haskell.
 
This page aims to cover some of its differences from Haskell.
   
== Syntactic differences ==
+
== Conceptual differences ==
  +
  +
OCaml is strict by default, although it has some facility for introducing laziness.
  +
 
OCaml's <tt>let</tt> is non-recursive by default, but has the form <tt>let rec</tt> for defining recursive 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.
  +
  +
== Syntactic dictionary ==
   
 
{| class="wikitable"
 
{| class="wikitable"
  +
|
| Language
 
| Haskell
+
| '''Haskell'''
| OCaml
+
| '''OCaml'''
| Comments
+
| '''Comments'''
 
|-
 
|-
 
| Anonymous functions
 
| Anonymous functions
Line 35: Line 43:
 
|
 
|
 
int, bool, float * char, 'a
 
int, bool, float * char, 'a
| <tt>float</tt> is a double type
+
| <tt>float</tt> is double-precision
 
|-
 
|-
 
| Type signatures
 
| Type signatures
Line 42: Line 50:
 
|
 
|
 
const : 'a -> 'b -> 'a
 
const : 'a -> 'b -> 'a
| Signatures usually omitted in OCaml
+
| Signatures usually omitted in OCaml implementations, may be provided separately in interface files
 
|-
 
|-
 
| Type declarations
 
| Type declarations
Line 54: Line 62:
 
and y = C ('a', true)
 
and y = C ('a', true)
 
|
 
|
  +
|-
  +
| Parametrised types
  +
|
  +
data DList a = MkDList ([a] -> [a])
  +
data Either a b = Left a | Right b
  +
|
  +
type 'a dlist = MkDList of ('a list -> 'a list)
  +
type ('a, 'b) either = Left of 'a | Right of 'b
 
|-
 
|-
 
| Pattern matching
 
| Pattern matching
 
|
 
|
 
case x of
 
case x of
A x -> ...
+
B x
  +
<nowiki>| x > 0 -> ...
  +
|</nowiki> otherwise -> ...
 
C a b -> ...
 
C a b -> ...
  +
  +
case Left () of
  +
Left x -> x
  +
Right x -> x
 
|
 
|
 
match x with
 
match x with
B x -> ...
+
B x when x > 0 -> ...
C (a, b) -> ...
+
<nowiki>| B x -> ...
  +
|</nowiki> C (a, b) -> ...
  +
  +
match Left () with
  +
Left x <nowiki>|</nowiki> Right x -> x
  +
|
 
|}
 
|}
 
== 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 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.
 

Latest revision as of 16:34, 15 May 2023

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.

Conceptual differences

OCaml is strict by default, although it has some facility for introducing laziness.

OCaml's let is non-recursive by default, but has the form let rec for defining recursive 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.

Syntactic dictionary

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 double-precision
Type signatures
const :: a -> b -> a
const : 'a -> 'b -> 'a
Signatures usually omitted in OCaml implementations, may be provided separately in interface files
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 DList a = MkDList ([a] -> [a])
data Either a b = Left a | Right b
type 'a dlist = MkDList of ('a list -> 'a list)
type ('a, 'b) either = Left of 'a | Right of 'b
Pattern matching
case x of
  B x
    | x > 0 -> ...
    | otherwise -> ...
  C a b -> ...
case Left () of
  Left x -> x
  Right x -> x
match x with
    B x when x > 0 -> ...
  | B x -> ...
  | C (a, b) -> ...
match Left () with
  Left x | Right x -> x