Overview
ML is often referred to as an impure functional language, because it permits side-effects, and therefore imperative programming, unlike purely functional programming languages such as Haskell. For this reason it's also called a Multi-paradigm programming language.Features of ML include a call-by-value evaluation strategy, first class functions, automatic memory management through garbage collection, parametric polymorphism, static typing, type inference, algebraic data types, pattern matching, and exception handling.
Unlike Haskell, ML uses eager evaluation, which means that all subexpressions are always evaluated. One result of this is that you cannot use infinite lists per se. However, lazy evaluation and hence infinite datastructures like lists can be simulated, through use of anonymous functions.
Today there are several languages in the ML family; the two major dialects are Standard ML and Caml, but others exist, including F# - an open research project that targets the Microsoft .NET platform. Ideas from ML have influenced numerous other languages, like Haskell, Cyclone, and Nemerle.
ML's strengths are mostly applied in language design and manipulation (compilers, analyzers, theorem provers), but it is a general-purpose language also used in bioinformatics, financial systems, and applications including a genealogical database, a peer-to-peer client/server program, etc.
ML uses static scoping rules.
Examples of ML
Anatomy of an ML function
The "Hello World" of functional languages is the factorial function. Expressed as pure ML:fun fac (0 : int) : int = 1
| fac (n : int) : int = n * fac (n-1)
This describes the factorial as a recursive function, with a single terminating base case. It is similar to the descriptions of factorials found in mathematics textbooks. Much of ML code is similar to mathematics in facility and syntax.
Part of the definition shown is optional, and describes the types of this function. The notation E : t can be read as expression E has type t. For instance, the argument n is assigned type integer (int), and the result of applying fac to n (fac (n)) also has type integer. The function fac as a whole then has type function from integer to integer (int -> int). Thanks to type inference, the type annotations can be omitted and will be derived by the compiler. Rewritten without the type annotations, the example looks like:
fun fac 0 = 1
| fac n = n * fac (n-1)
The function also relies on pattern matching, an important part of ML programming. Note that parameters of a function are not necessarily in parentheses but separated by spaces. When the function's argument is 0 (zero) it will return the integer 1 (one). For all other cases the second line is tried. This is the recursion, and executes the function again until the base case is reached.
Reverse Function:
fun reverse ls = let
fun reverseInner nil acc = acc
| reverseInner (hd::tail) acc = reverseInner tail (hd::acc)
in
reverseInner ls nil
end
See also
Dialects
- Caml, a dialect of ML, which became...
- * Objective Caml, the famous dialect of Caml with support for object-oriented programming
- Standard ML, a dialect of ML with a formal semantics
- Alice (programming language)
- F#
Books
- The Definition of Standard ML, Robin Milner, Mads Tofte, Robert Harper, MIT Press 1990
- The Definition of Standard ML (Revised), Robin Milner, Mads Tofte, Robert Harper, David MacQueen, MIT Press 1997. ISBN 0-262-63181-4
- Commentary on Standard ML, Robin Milner, Mads Tofte, MIT Press 1997. ISBN 0-262-63137-7
- Robert Harper: "Programming in Standard ML", Carnegie Mellon University, 2005.
References
External links
- Moscow ML, a popular implementation of Standard ML
- Standard ML of New Jersey, another popular implementation
- F#, an ML implementation using the Microsoft .NET framework
- MLton, a whole-program optimizing Standard ML compiler
- sML, Successor ML
This article is licensed under the GNU Free Documentation License.
Last updated on Friday July 25, 2008 at 13:49:01 PDT (GMT -0700)
View this article at Wikipedia.org - Edit this article at Wikipedia.org - Donate to the Wikimedia Foundation
Copyright © 2008, Dictionary.com, LLC. All rights reserved.











