From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 29D08BC57 for ; Wed, 14 Apr 2010 20:13:58 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvcCAE6ixUvZSMDqkWdsb2JhbACbVhUBAQEBCQsKBxEDH71mhQ0E X-IronPort-AV: E=Sophos;i="4.52,205,1270418400"; d="scan'208";a="60715534" Received: from fmmailgate03.web.de ([217.72.192.234]) by mail4-smtp-sop.national.inria.fr with ESMTP; 14 Apr 2010 20:13:57 +0200 Received: from smtp08.web.de (fmsmtp08.dlan.cinetic.de [172.20.5.216]) by fmmailgate03.web.de (Postfix) with ESMTP id 4890014D77790; Wed, 14 Apr 2010 20:13:57 +0200 (CEST) Received: from [78.43.204.177] (helo=frosties.localdomain) by smtp08.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #4) id 1O2765-0007Ko-00; Wed, 14 Apr 2010 20:13:57 +0200 Received: from mrvn by frosties.localdomain with local (Exim 4.71) (envelope-from ) id 1O2763-0006iJ-9I; Wed, 14 Apr 2010 20:13:55 +0200 From: Goswin von Brederlow To: Dmitry Bely Cc: Caml List Subject: Re: [Caml-list] Optimizing Float Ref's References: <4A9F9015.2010408@inria.fr> <4BB39B7C.6050402@frisch.fr> Date: Wed, 14 Apr 2010 20:13:55 +0200 In-Reply-To: (Dmitry Bely's message of "Wed, 31 Mar 2010 23:18:36 +0400") Message-ID: <87633tnbq4.fsf@frosties.localdomain> User-Agent: Gnus/5.110009 (No Gnus v0.9) XEmacs/21.4.22 (linux, no MULE) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: goswin-v-b@web.de X-Sender: goswin-v-b@web.de X-Provags-ID: V01U2FsdGVkX181F6YAyh5mNi7s8QohgxPfxkUIQS2gCFUWTcXx LujtcvQ95D1vs6KtJdbJdQMbmMgmC/4Cor6Ljjc6LaNfo7Z6A5 DdAOfhbfI= X-Spam: no; 0.00; ref's:01 val:01 val:01 mfg:98 rec:01 rewrite:01 recursively:01 dmitry:01 dmitry:01 caml-list:01 writes:01 bely:01 bely:01 len:02 len:02 Dmitry Bely writes: > rewrite the function to get rid of it? My best achievement so far is > > let max_val (a:float array) = > let m = [|-.min_float|] in > for i = 0 to (Array.length a) - 1 do > if a.(i) > m.(0) > then m.(0) <- a.(i) > done; > m.(0) > > but it looks ugly... > > - Dmitry Bely Why not store the index of the max element instead of the float itself? Recursively, to avoid the ref as well, this could look like this: let max_val (a:float array) = let len = Array.length a in let rec loop max i = if i = len then a.(max) else if a.(max) > a.(i) then loop max (i + 1) else loop i (i + 1) in if len = 0 then -.min_float else loop 0 1 MfG Goswin