Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Standard ML
(section)
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!
===Partial application=== Curried functions have many applications, such as eliminating redundant code. For example, a module may require functions of type {{code|lang=sml|a -> b}}, but it is more convenient to write functions of type {{code|lang=sml|a * c -> b}} where there is a fixed relationship between the objects of type {{code|a}} and {{code|c}}. A function of type {{code|lang=sml|c -> (a * c -> b) -> a -> b}} can factor out this commonality. This is an example of the [[adapter pattern]].{{Citation needed|date=May 2015}} In this example, {{code|lang=sml|fun d}} computes the numerical derivative of a given function {{code|f}} at point {{code|x}}: <syntaxhighlight lang="sml" highlight="1"> - fun d delta f x = (f (x + delta) - f (x - delta)) / (2.0 * delta) val d = fn : real -> (real -> real) -> real -> real </syntaxhighlight> The type of {{code|lang=sml|fun d}} indicates that it maps a "float" onto a function with the type {{code|lang=sml|(real -> real) -> real -> real}}. This allows us to partially apply arguments, known as [[currying]]. In this case, function {{code|d}} can be specialised by partially applying it with the argument {{code|delta}}. A good choice for {{code|delta}} when using this algorithm is the cube root of the [[machine epsilon]].{{Citation needed|date=August 2008}} <syntaxhighlight lang="sml" highlight="1"> - val d' = d 1E~8; val d' = fn : (real -> real) -> real -> real </syntaxhighlight> The inferred type indicates that {{code|d'}} expects a function with the type {{code|lang=sml|real -> real}} as its first argument. We can compute an approximation to the derivative of <math>f(x) = x^3-x-1</math> at <math>x=3</math>. The correct answer is <math>f'(3) = 27-1 = 26</math>. <syntaxhighlight lang="sml" highlight="1"> - d' (fn x => x * x * x - x - 1.0) 3.0; val it = 25.9999996644 : real </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)