
I'm a newbie in Haskell. I tryed to compare performance of GHC compiled code and Ocaml compiled code on a small example: test n cosx | n==0 = cosx | otherwise = test (n-1) (cosx+cos(cosx)) main = print ( test 10000000 0.0 ) I compiled it on Win NT : ghc -o test_haskell.exe test_haskell.hs When I run the program I got the following message: Stack space overflow: current size 1048576 bytes. Use `+RTS -Ksize' to increase it. Hugs 98 (December 2001) version just fails with "Application Error" message. Does a stack usage depends on number of recursive calls? Is there a tail recursion optimization? In Ocaml version of this program everything goes well. The program produced the result without any complains. Oleg Galbert

On Fri, 21 Dec 2001, Oleg Galbert wrote:
I'm a newbie in Haskell. I tryed to compare performance of GHC compiled code and Ocaml compiled code on a small example:
test n cosx | n==0 = cosx | otherwise = test (n-1) (cosx+cos(cosx))
main = print ( test 10000000 0.0 )
I compiled it on Win NT :
ghc -o test_haskell.exe test_haskell.hs
When I run the program I got the following message:
Stack space overflow: current size 1048576 bytes. Use `+RTS -Ksize' to increase it.
Hugs 98 (December 2001) version just fails with "Application Error" message.
Does a stack usage depends on number of recursive calls? Is there a tail recursion optimization?
In Ocaml version of this program everything goes well. The program produced the result without any complains.
Oleg Galbert
_______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
-- Matthew Bentham, ART ltd., Mount Pleasant House, Cambridge CB3 ORN Direct tel: +44 1223 578547 If this message is PGP signed, you may verify its signature with my public key from http://www-stu.christs.cam.ac.uk/~mjb67/

On Fri, Dec 21, 2001 at 04:22:42PM -0600, Oleg Galbert wrote:
test n cosx | n==0 = cosx | otherwise = test (n-1) (cosx+cos(cosx))
Functions by default are lazy, i.e. they does not compute the number, but make a note to do it later. In the example you presented, the value of cosx is not needed until the call for "test (n-1) (cosx+cos(cosx))". So, as a result of "test 10000000 0.0" you get a (...+cos(0.0+cos(0.0)))...). That's not surprising it crashes the program.
test1 n cosx | n==0 = cosx | otherwise = cosx `seq` test1 (n-1) (cosx+cos(cosx))
Here, you make sure that cosx is counted up to a number. This function works OK. read http://haskell.org/ghc/docs/latest/set/faster.html
main = print ( test 10000000 0.0 )
Oleg Galbert
Max.
participants (3)
-
Matthew Bentham
-
Max A.K.
-
Oleg Galbert