Horner's rule for polynomial evaluation: Difference between revisions

(added ocaml)
Line 156:
 
<lang ocaml># let horner coeffs x =
List.fold_left (fun acc coef -> (acc * x) + coef) 0 (List.rev coeffs) ;;
val horner : int list -> int -> int = <fun>
 
Line 162:
horner coeffs 3 ;;
- : int = 128</lang>
It's also possible to do fold_right instead of reversing and doing fold_left; but fold_right is not tail-recursive.
 
 
=={{header|Oz}}==
Anonymous user