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 PAA24399; Sun, 8 Jun 2003 15:11:35 +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 PAA20164 for ; Sun, 8 Jun 2003 15:11:34 +0200 (MET DST) Received: from marionberry.cc.columbia.edu (marionberry.cc.columbia.edu [128.59.59.100]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h58DBXH05147 for ; Sun, 8 Jun 2003 15:11:33 +0200 (MET DST) Received: from tw304h3.cpmc.columbia.edu (tw304h3.cpmc.columbia.edu [156.111.84.180]) (user=ot14 mech=LOGIN bits=0) by marionberry.cc.columbia.edu (8.12.8p1/8.12.8) with ESMTP id h58DBV7C024253 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NOT); Sun, 8 Jun 2003 09:11:32 -0400 (EDT) From: Oleg Trott To: "TBraibant" , Subject: Re: [Caml-list] Function composition in CAML Date: Sun, 8 Jun 2003 09:11:18 -0400 User-Agent: KMail/1.5.1 References: <000a01c32db1$041915c0$0100a8c0@r2d2> In-Reply-To: <000a01c32db1$041915c0$0100a8c0@r2d2> MIME-Version: 1.0 Content-Type: text/plain; charset="koi8-u" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200306080911.19208.oleg_trott@columbia.edu> X-No-Spam-Score: Local X-Scanned-By: MIMEDefang 2.32 (www . roaringpenguin . com / mimedefang) X-Spam: no; 0.00; oleg:01 caml-list:01 bug:01 caml:01 mutable:01 rec:01 match:02 wrote:03 let:04 array:04 infinite:05 implement:05 efficient:05 seems:05 variables:06 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Sunday 08 June 2003 07:27 am, TBraibant wrote: > Hello > > 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 > (I know that there is a more efficient way to do this calculation (P(x) in > fact), but it is just an expermimentation) > > 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? > > Thank you The computer does not crash, you are merely entrering an infinite loop because, with function composition, you couldn't keep track of the mutable variables in your code. A possible solution is: let eval p x = let rec aux p prod sum = match p with | [] -> sum | a :: b -> aux b (x*prod) (a*prod + sum) in aux p 1 0 ;; "eval" does not implement Horner's rule. The latter sounds like homework :) -- Oleg Trott ------------------- 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