From: Goswin von Brederlow <goswin-v-b@web.de>
To: Dmitry Bely <dmitry.bely@gmail.com>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Optimizing Float Ref's
Date: Wed, 14 Apr 2010 20:13:55 +0200 [thread overview]
Message-ID: <87633tnbq4.fsf@frosties.localdomain> (raw)
In-Reply-To: <g2l90823c941003311218o21b3c207tfea7b3984f5ee93b@mail.gmail.com> (Dmitry Bely's message of "Wed, 31 Mar 2010 23:18:36 +0400")
Dmitry Bely <dmitry.bely@gmail.com> 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
prev parent reply other threads:[~2010-04-14 18:13 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-08-28 20:32 Will M Farr
2009-08-30 19:43 ` [Caml-list] " Yaron Minsky
2009-08-31 14:09 ` Till Varoquaux
2009-08-31 14:51 ` Will M Farr
2009-08-31 17:30 ` Jon Harrop
2009-08-31 17:15 ` Jon Harrop
2009-09-03 9:44 ` Xavier Leroy
2009-09-03 10:15 ` Will M Farr
2010-03-31 17:21 ` Dmitry Bely
[not found] ` <p2tc7e4e9f1003311055xce0919wac2118aa3c05f1cb@mail.gmail.com>
2010-03-31 18:28 ` Dmitry Bely
2010-03-31 18:59 ` Alain Frisch
2010-03-31 19:18 ` Dmitry Bely
[not found] ` <m2lfbd71dab1003311252v5bda5d13vc2146d2d24270847@mail.gmail.com>
2010-03-31 20:00 ` Dmitry Bely
2010-04-14 18:13 ` Goswin von Brederlow [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87633tnbq4.fsf@frosties.localdomain \
--to=goswin-v-b@web.de \
--cc=caml-list@inria.fr \
--cc=dmitry.bely@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox