Merge pull request #430 from sshine/master

[standard-ml/en-en] Removing syntax errors and fixing solve2 function
This commit is contained in:
Nami-Doc 2013-12-02 23:23:54 -08:00
commit db072899ff

View File

@ -199,17 +199,17 @@ val hmm = answer "What is the meaning of life, the universe and everything?"
(* Functions can take several arguments by taking one tuples as argument: *) (* Functions can take several arguments by taking one tuples as argument: *)
fun solve2 (a : real, b : real, c : real) = fun solve2 (a : real, b : real, c : real) =
( Math.sqrt (~b + Math.sqrt(b * b - 4.0*a*c) / (2.0 * a), ( (~b + Math.sqrt(b * b - 4.0*a*c)) / (2.0 * a),
Math.sqrt (~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 (* 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": *) to save and re-use the result the first time. We can use "let-bindings": *)
fun solve2 (a : real, b : real, c : real) = 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 d val sqr = Math.sqrt discr
val denom = 2.0 * a val denom = 2.0 * a
in (~b + sq / denom, in ((~b + sqr) / denom,
~b - sq / denom) end (~b - sqr) / denom) end
(* Pattern matching is a funky part of functional programming. It is an (* Pattern matching is a funky part of functional programming. It is an
@ -276,7 +276,7 @@ fun say(col) =
fun say Red = "You are red!" fun say Red = "You are red!"
| say Green = "You are green!" | say Green = "You are green!"
| say Blue = "You are blue!" | say Blue = "You are blue!"
| _ = raise Fail "Unknown color" | say _ = raise Fail "Unknown color"
(* Here is a binary tree datatype *) (* Here is a binary tree datatype *)
@ -321,7 +321,7 @@ val test_poem = readPoem "roses.txt" (* gives [ "Roses are red,",
"Violets are blue.", "Violets are blue.",
"I have a gun.", "I have a gun.",
"Get in the van." ] *) "Get in the van." ] *)
~~~~ ```
## Further learning ## Further learning