From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA10691; Tue, 10 Jun 2003 10:49:20 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id KAA10790 for ; Tue, 10 Jun 2003 10:49:17 +0200 (MET DST) Received: from decis.be ([194.78.219.157]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h5A8nGH15523 for ; Tue, 10 Jun 2003 10:49:17 +0200 (MET DST) Received: from decis.be ([192.168.0.20]) by decis.be ([194.78.219.157]) with SMTP (MDaemon.PRO.v6.5.0.R) for ; Tue, 10 Jun 2003 10:53:48 +0200 Message-ID: <3EE59B8B.81D727FB@decis.be> Date: Tue, 10 Jun 2003 10:49:15 +0200 From: Frederic van der Plancke Reply-To: fvdp@decis.be, fplancke@hotmail.com Organization: Decis X-Mailer: Mozilla 4.77 [en] (Windows NT 5.0; U) X-Accept-Language: en MIME-Version: 1.0 CC: caml-list@inria.fr Subject: Re: [Caml-list] Function composition in CAML References: <000a01c32db1$041915c0$0100a8c0@r2d2> Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Authenticated-Sender: fplancke@decis.be X-MDRemoteIP: 192.168.0.20 X-Return-Path: fvdp@decis.be X-MDaemon-Deliver-To: caml-list@inria.fr X-Spam: no; 0.00; plancke:01 fvdp:01 caml-list:01 bug:01 recursion:01 out-of-bound:01 caml:01 recursive:03 argument:03 let:04 rewrite:04 array:04 infinite:05 seems:05 maybe:06 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > 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 caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners