From: Judicael.Courant@lip.ens-lyon.fr (Judicael Courant)
To: pn@univ-angers.fr
Cc: caml-list@pauillac.inria.fr, osman.buyukisik@ae.ge.com
Subject: Re: vector dot multiply
Date: Fri, 9 Jun 1995 12:42:19 +0200 [thread overview]
Message-ID: <9506091042.AA18436@champagne.ens-lyon.fr> (raw)
In-Reply-To: <199506090729.JAA03487@vink.Univ-Angers.Fr> (message from Pascal Nicolas on Fri, 9 Jun 95 9:29:41 METDST)
> In order to avoid to (re)compute the length of a at each recursive call, you
> can modify a little your function as follows
>
> let dot a b = let rec dot_aux a b i sum L =
> if i < L then
> dot_aux a b (i+1) (sum +. (a.(i) *. b.(i))) L
> else
> sum
> in dot_aux a b 0 0.0 (vect_length a) ;;
in order to avoid adding a new parameter, one can also write :
let dot a b =
let rec dot_aux i sum =
match i with
-1 -> sum
| i -> dot_aux (i-1) (sum +. (a.(i) *. b.(i)))
else
sum
in dot_aux ((vect_length a)-1) 0.0 ;;
let dot a b = let rec dot_aux i sum =
if i >= 0 then
dot_aux (i-1) (sum +. (a.(i) *. b.(i))) L
else
sum
in dot_aux ((vect_length a)-1) 0.0 ;;
(notice that it is a classical way to obtain a tail-recursion from
a non-terminal one : the previous form is obtained from
let dot a b
let rec dot_aux i =
match i with
0 -> 0
| k -> (a.(i) * b.(i)) + (dot_aux (i-1))
in dot_aux ((vect_length a)-1);;
as
let fact n =
match n with
0 -> 1
| k -> k*(fact (k-1))
;;
becomes
let fact n =
let rec fact_aux n prod =
match n with
0 -> prod
| k -> k*(fact_aux (k-1))
;;
)
Judicael Courant
--
|| Judicael.Courant@ens-lyon.fr \\ ``Big Brother is watching \\
|| http://www.ens-lyon.fr/~jcourant/ \\ YOU ! '' \\
|| tel : 72 72 85 82 \\ G. Orwell, 1984 \\
next prev parent reply other threads:[~1995-06-09 11:55 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
1995-06-08 17:16 U-E59264-Osman Buyukisik
1995-06-08 18:29 ` Pierre Weis
1995-06-08 23:02 ` Ascander Suarez
1995-06-08 23:17 ` Bob Buckley
1995-06-09 7:29 ` Pascal Nicolas
1995-06-09 10:42 ` Judicael Courant [this message]
1995-06-09 11:50 Chet Murthy
1995-06-09 13:20 ` nikhil
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=9506091042.AA18436@champagne.ens-lyon.fr \
--to=judicael.courant@lip.ens-lyon.fr \
--cc=caml-list@pauillac.inria.fr \
--cc=osman.buyukisik@ae.ge.com \
--cc=pn@univ-angers.fr \
/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