- , , , . SML, Java, , .
, , . , . if-then-else, , . :
fun horner ([] , x) = 0.0
| horner (n::ns, x) = ...
. -, - n , ns - . -, .
. horner , result , . , , ref . .
fun horner ([] , x, acc) = acc
| horner (n::ns, x, acc) = ...
, , , :
fun horner' ([] , x, acc) = acc
| horner' (n::ns, x, acc) = ...
fun horner (xs, x) = horner' (xs, x, 0.0)
, , SML , . , let:
fun horner (xs, x) = let
fun horner' ([] , x, acc) = acc
| horner' (n::ns, x, acc) = ...
in
horner' (xs, x, 0.0)
end
, horner'.
fun horner (xs, x) = let
fun horner' ([] , x, acc) = acc
| horner' (n::ns, x, acc) = horner' (ns, x, n + x * acc)
in
horner' (xs, x, 0.0)
end
, horner:
horner ([3.0, 2.0, 4.0], 2.0) ~> horner' ([3.0, 2.0, 4.0], 2.0, 0.0)
~> horner' ([2.0, 4.0] , 2.0, 3.0 + 2.0 * 0.0)
~> horner' ([2.0, 4.0] , 2.0, 3.0)
~> horner' ([4.0] , 2.0, 2.0 + 2.0 * 3.0)
~> horner' ([4.0] , 2.0, 8.0)
~> horner' ([] , 2.0, 4.0 + 2.0 * 8.0)
~> horner' ([] , 2.0, 20.0)
~> 20.0