Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Function composition in CAML
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Frederic van der Plancke <fvdp@d...>
Subject: Re: [Caml-list] Function composition in CAML
> I have made some experimentations, but I can't find where is a bug in a 
> simple piece of code

> I represent a polynome by a list of its coefficents
> let horner p x =
>   let v= Array.of_list p in
>   let n = Array.length v in
>   let r = ref n in 
>   let f = ref (function u ->u  ) in
>     while !r <> 0 do
>       f := (function u -> !f( v.(!r)+ x*u));
>       r := !r -1 ;
>     done;
>     !f(0)
> ;;
> In theory, the !f(0) call shall give me P(x)...
> But it seems that the computer crash, and can't handle this line of code...
> Someone has an idea?

Subtle !
There is an infinite recursion: operator "!" before "f" is evaluated when
the code where it lies is evaluated, that is when f is called, 
hence "!f" represents the last created function f and not the one you want. 
You should evaluate "!f" outside of (function u -> ....), or (better) 
rewrite your loop using a recursive function taking "the current r" and
(maybe) "the current f" as argument(s).

(There's also an independent bug which will yield an out-of-bound array 
access error...)

Frédéric vdP

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: