[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
This commit is contained in:
Yu Zhang 2017-02-10 01:54:31 -06:00 committed by ven
parent 45de0120d6
commit d8a3184a73

View File

@ -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) =