summaryrefslogtreecommitdiffhomepage
path: root/standard-ml.html.markdown
diff options
context:
space:
mode:
authorYu Zhang <583181285@qq.com>2017-02-10 01:54:31 -0600
committerven <vendethiel@hotmail.fr>2017-02-10 08:54:31 +0100
commitd8a3184a73cfbb3be3ab99e7d7e3ebc577d06494 (patch)
tree1eeb1f1709e82437351051c51bbf2b6519db7f47 /standard-ml.html.markdown
parent45de0120d641cfaac0bb60b25a24782ec106e719 (diff)
[standard-ml/en-en] Format fixing and some APIs (#2645)
* [standard-ml/en-en] Format fixing * [standard-ml/en-en] Add descriptions for `map` and `foldl` * [standard-ml/en-en] Add myself as a contributor * review
Diffstat (limited to 'standard-ml.html.markdown')
-rw-r--r--standard-ml.html.markdown21
1 files changed, 15 insertions, 6 deletions
diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown
index 133e4f54..c9eb2a2e 100644
--- a/standard-ml.html.markdown
+++ b/standard-ml.html.markdown
@@ -235,17 +235,18 @@ val hmm = answer "What is the meaning of life, the universe and everything?"
(* Functions can take several arguments by taking one tuples as argument: *)
fun solve2 (a : real, b : real, c : real) =
- ( (~b + Math.sqrt(b * b - 4.0*a*c)) / (2.0 * a),
- (~b - Math.sqrt(b * b - 4.0*a*c)) / (2.0 * a) )
+ ((~b + Math.sqrt(b * b - 4.0 * a * c)) / (2.0 * a),
+ (~b - Math.sqrt(b * b - 4.0 * a * c)) / (2.0 * a))
(* Sometimes, the same computation is carried out several times. It makes sense
to save and re-use the result the first time. We can use "let-bindings": *)
fun solve2 (a : real, b : real, c : real) =
- let val discr = b * b - 4.0*a*c
+ let val discr = b * b - 4.0 * a * c
val sqr = Math.sqrt discr
val denom = 2.0 * a
in ((~b + sqr) / denom,
- (~b - sqr) / denom) end
+ (~b - sqr) / denom)
+ end
(* Pattern matching is a funky part of functional programming. It is an
@@ -292,6 +293,9 @@ val thermometer =
val some_result = (fn x => thermometer (x - 5) ^ thermometer (x + 5)) 37
(* Here is a higher-order function that works on lists (a list combinator) *)
+(* map f l
+ applies f to each element of l from left to right,
+ returning the list of results. *)
val readings = [ 34, 39, 37, 38, 35, 36, 37, 37, 37 ] (* first an int list *)
val opinions = List.map thermometer readings (* gives [ "Cold", "Warm", ... ] *)
@@ -324,7 +328,11 @@ val n = op + (5, 5) (* n is now 10 *)
(* 'op' is useful when combined with high order functions because they expect
functions and not operators as arguments. Most operators are really just
infix functions. *)
-val sum_of_numbers = foldl op+ 0 [1,2,3,4,5]
+(* foldl f init [x1, x2, ..., xn]
+ returns
+ f(xn, ...f(x2, f(x1, init))...)
+ or init if the list is empty. *)
+val sum_of_numbers = foldl op+ 0 [1, 2, 3, 4, 5]
(* Datatypes are useful for creating both simple and complex structures *)
@@ -407,7 +415,8 @@ fun writePoem(filename) =
let val file = TextIO.openOut(filename)
val _ = TextIO.output(file, "Roses are red,\nViolets are blue.\n")
val _ = TextIO.output(file, "I have a gun.\nGet in the van.\n")
- in TextIO.closeOut(file) end
+ in TextIO.closeOut(file)
+ end
(* Read a nice poem from a file into a list of strings *)
fun readPoem(filename) =