Stack overflow in OCaml and F# but not in Haskell
Posted
by Fernand Pajot
on Stack Overflow
See other posts from Stack Overflow
or by Fernand Pajot
Published on 2010-02-20T14:31:12Z
Indexed on
2010/05/11
2:34 UTC
Read the original article
Hit count: 492
I've been comparing for fun different languages for speed in execution of the following program: for i from 1 to 1000000 sum the product i*(sqrt i)
One of my implementations (not the only one) is constructing a list [1..1000000] and then folding with a specific funtion.
The program works fine and fast in Haskell (even when using foldl and not foldl') but stack overflows in OCaml and F#.
Here is the Haskell code:
test = foldl (\ a b -> a + b * (sqrt b)) 0
create 0 = []
create n = n:(create (n-1))
main = print (test (create 1000000))
And here is the OCaml one:
let test = List.fold_left (fun a b -> a +. (float_of_int b) *. (sqrt (float_of_int b))) 0. ;;
let rec create = function
| 0 -> []
| n -> n::(create (n-1)) ;;
print_float (test (create 1000000));;
Why does the OCaml/F# implementation stack overflows?
© Stack Overflow or respective owner