Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
ML (programming language)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Factorial=== The [[factorial]] function expressed as pure ML: <syntaxhighlight lang="sml"> fun fac (0 : int) : int = 1 | fac (n : int) : int = n * fac (n - 1) </syntaxhighlight> 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 fac (n : int), the result of applying fac to the integer n, also has type integer. The function fac as a whole then has type ''function from integer to integer'' (int -> int), that is, fac accepts an integer as an argument and returns an integer result. 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: <syntaxhighlight lang="sml"> fun fac 0 = 1 | fac n = n * fac (n - 1) </syntaxhighlight> 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 (computer science)|recursion]], and executes the function again until the base case is reached. This implementation of the factorial function is not guaranteed to terminate, since a negative argument causes an [[infinite descending chain]] of recursive calls. A more robust implementation would check for a nonnegative argument before recursing, as follows: <syntaxhighlight lang="sml"> fun fact n = let fun fac 0 = 1 | fac n = n * fac (n - 1) in if (n < 0) then raise Domain else fac n end </syntaxhighlight> The problematic case (when n is negative) demonstrates a use of ML's exception system. The function can be improved further by writing its inner loop as a [[tail call]], such that the [[call stack]] need not grow in proportion to the number of function calls. This is achieved by adding an extra, ''accumulator'', parameter to the inner function. At last, we arrive at <syntaxhighlight lang="sml"> fun fact n = let fun fac 0 acc = acc | fac n acc = fac (n - 1) (n * acc) in if (n < 0) then raise Domain else fac n 1 end </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
ML (programming language)
(section)
Add topic